[Django]-Django Rest Framework – AssertionError in tests

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")

Leave a comment