Django unit test response context is None

后端 未结 2 1400
小蘑菇
小蘑菇 2021-01-23 10:44

Why is response.context None in the following tests? I\'ve checked response.content and it is \'\'. if I remove the assertIsNotNone line I get a TypeError: \'NoneType\' object

相关标签:
2条回答
  • 2021-01-23 11:06

    Quick workaround

    There is another member dict of Context class (derived from SimpleTemplateResponse) which is called context_data. Using this instead of just context will give the expected context dict (django 1.9). But be aware, it's defined to be deprecated since django 1.8. So use it at your own risk. Unfortunately I didn't research a proper alternative until now.

    Background

    The existence of the context attribute of a response seems to be dependent on the type of template engine (templates) you are using. If you go for another engine than the one coming with django (default) you likely find the dict at the context_data attribute

    References

    • Testing responses
    • context_data
    0 讨论(0)
  • 2021-01-23 11:23

    I was having this issue and to fix it did the following in the setUp function:

    from django.test.utils import setup_test_environment
    setup_test_environment()
    
    0 讨论(0)
提交回复
热议问题