[Answered ]-How to pass data from request body in unit test django

1👍

OK, so self.client.get takes data which should be a dictionary. I think you want to do the following…

def test_list_three_review(self):
    [ReviewsFactory.create() for i in range(10)]
    data = {'type_of_review':'Team'}
    response = self.client.get(self.url2, data)
    self.assertEqual(response.status_code, status.HTTP_200_OK)

Not exactly sure why you want to include a list of objects in the request since your get_queryset method overrides it anyway.

0👍

You are passing data as query string parameter. In-order to parse it in view you could use request.GET.get("type_of_review")

your get_queryset() method will become

def get_queryset(self):

        type_of_review = self.request.GET.get('type_of_review')
        queryset = Reviews.objects.filter(type_of_review=type_of_review).order_by('-id')[:3]
        return queryset

GET request is not for passing json data, if you need to pass data as json anyway you could use POST method

then you can pass data like

def test_list_three_review(self):

    self.data = [ReviewsFactory.create() for i in range(10)]
    data = {"type_of_review": "Team"}
    response = self.client.post(self.url2, data)
    self.assertEqual(response.status_code, status.HTTP_200_OK)

and your get_queryset method will work as it is.

Leave a comment