Enzo Massaki
Enzo Massaki

Reputation: 311

<HttpResponseNotFound status_code=404, "text/html"> in django-test

I'm new to unit testing and I've been trying to test a GET method of the card game that I've built.

My TestCase is the following:

def test_rooms(self):
    c = APIClient()
    room_id = PokerRoom.objects.get(name='planning').id
    request = c.get('http://127.0.0.1:8000/room/{}'.format(room_id))
    print(request)

The id is a UUID that's why I'm using the room_id method.

My url:

    path('room/<int:pk>', room),

where room is a @api_view(['GET']) method and pk is the id of the room. But when I try to test it, an error occurs:

<HttpResponseNotFound status_code=404, "text/html">

Checked if the room exists in the test database and it exists, now I don't know what is happening. Can someone help me?

Upvotes: 0

Views: 846

Answers (1)

Can you add more details to your code above, such as how the test suite has been created, how is the data set up, etc? One problem I am noticing straight away is how the request is being made. Why are you using a complete URL? If you are using the Django/DRF test API client, you should use the view path URL instead of the complete URL.

Reference: https://docs.djangoproject.com/en/4.0/topics/testing/tools/#overview-and-a-quick-example

Upvotes: 2

Related Questions