Django Rest Framework – How to test ViewSet?

I think I found the correct syntax, but not sure if it is conventional (still new to Django):

def test_view_set(self):
    request = APIRequestFactory().get("")
    cat_detail = CatViewSet.as_view({'get': 'retrieve'})
    cat = Cat.objects.create(name="bob")
    response = cat_detail(request, pk=cat.pk)
    self.assertEqual(response.status_code, 200)

So now this passes and I can assign request.user, which allows me to customize the retrieve method under CatViewSet to consider the user.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)