Search code examples
djangodjango-tests

Django: How to test view context with RequestFactory?


I am writing unit tests and creating request objects with RequestFactory because almost all of my app is behind login and I need to set request.user but this way cannot access the context of the response to test the values my view sent as part of the response.

When using the Testing client I can get the context this way response.context_data['form'] and write asserts against it.

What is the proper way of testing views that require login? I also tried inspecting the response object with debugger and could not find anything resembling context.


Solution

  • I don't understand why you don't use the test client. It has support for logging a user in and then you can assert on the context as you want.