5👍
✅
Expanding on @linovias answer, the key is to pass the extra arguments to your view
response = (view, username='useri')
Full test
def test_details(self):
factory = APIRequestFactory()
view = AccountDetail.as_view()
#unauthenticated
url = reverse('account-detail', kwargs={'username': 'useri'})
request = factory.get(url)
response = view(request, username='useri')
self.assertEqual(response.status_code, 200)
5👍
RequestFactory is to unittest views. It does bypass the middleware and the url layer. Therefore you need to provide the additional arguments by yourself while with the ClientFactory urls.py do it for you.
Edit:
In other words,
response = view(request)
should be
response = view(request, "useri")
- [Django]-When does the queryset use its _result_cache, not hitting the database?
- [Django]-How to create a new table using model
- [Django]-Django URLResolver error
- [Django]-Django admin datetime widget (calendar) in a custom form
Source:stackexchange.com