Zahra Hosseini
Zahra Hosseini

Reputation: 596

Pass query params but got error that query params is required

I write some unit tests for my django app. but test failed, because of that query params missed but I passed the query params.

This is one of my tests:

    def test_delete_card(self):  
        url = reverse("postideas-urls:DeleteCard")
        card_pk=self.test_card_second.id
        data3 = {
                'card_pk':card_pk
                }
        self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + self.token)
        response = self.client.delete(url,params=data3)
        print(response.__dict__)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

and this is my response.__dict___

... '_container': [b'{"error":"card_pk query param(s) are required"}'], '_is_rendered': True, 'data': {'error': 'card_pk query param(s) are required'}, 'exception': False, 'content_type': None, 'accepted_renderer': <rest_framework.renderers.JSONRenderer object at 0x7fb757f3aad0>, 'accepted_media_type': 'application/json', 'renderer_context': {'view': <postideas.views.DeleteCardView object at 0x7fb757f37d10>, 'args': (), 'kwargs': {}, 'request': <rest_framework.request.Request: DELETE '/api/v1/postideas/Delete_Card/'>, 'response': <Response status_code=400, ...'request': {'PATH_INFO': '/api/v1/postideas/Delete_Card/', 'REQUEST_METHOD': 'DELETE', 'SERVER_PORT': '80', 'wsgi.url_scheme': 'http', 'params': {'card_pk': 5}, 'QUERY_STRING': '', 'HTTP_AUTHORIZATION': 'Bearer ...

       

Upvotes: 2

Views: 842

Answers (1)

Abdul Aziz Barkat
Abdul Aziz Barkat

Reputation: 21802

You pass the data as params=data3 but it is supposed to be either the 2nd positional argument or the keyword argument data. Hence you can make the request as:

response = self.client.delete(url, data3)

OR

response = self.client.delete(url, data=data3)

Note: Also if you use the generic views by DRF the status code for a successful delete request is 204, hence the assert might need to be changed to:

self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

Upvotes: 1

Related Questions