Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest Framework Test case issue: 'HttpResponseNotAllowed' object has no attribute 'data'

My tests file code:

from rest_framework import status
from rest_framework.test import APITestCase

class CategoryTests(APITestCase):

  def test_create_create(self):

    url = '/category/add/'
    data = {"name":"Sports","description":"get live updates here"}
    response = self.client.post(url, data, format='json')
    self.assertEqual(response.data, data)

Error which I am getting:

Traceback (most recent call last):
File "/Users/test/webapp/apps/core/tests.py", line 16, in test_create_create
self.assertEqual(response.data, data)
AttributeError: 'HttpResponseNotAllowed' object has no attribute 'data'

Infact the tests are not even calling the exact api statements(I checked that using debug statements in api code). Please let me know what may be going wrong or you need any more information on this.

like image 765
Neo Avatar asked Mar 22 '26 07:03

Neo


2 Answers

Try using the DRF extended test client:

from rest_framework import status
from rest_framework.test import APITestCase, APIClient

class CategoryTests(APITestCase):
  client = APIClient()

  def test_create_create(self):

    url = '/category/add/'
    data = {"name":"Sports","description":"get live updates here"}
    response = self.client.post(url, data, format='json')
    self.assertEquals(response.data, data)
like image 173
Mark Galloway Avatar answered Mar 23 '26 23:03

Mark Galloway


Issue was with url, I correct it and it worked. So my url was actually

url = '/v1.0/category/add/'
like image 42
Neo Avatar answered Mar 23 '26 23:03

Neo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!