[Fixed]-Django Rest Framework Test Class Error

1πŸ‘

βœ…

The issue is in the below code:

def test_get_detail(self):
    ArtistTest._setup_add_record(self)
    response = self.client.get(url_3)
    self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

You cannot call instance method like ArtistTest._setup_add_record. You need to create an instance all call the method like ArtistTest()._setup_add_record

If you want to use the method is different class then go for inheritance/mixing:

class ArtistRecordSetupMixing():
    # For reusable of adding a single record
    # To be used for POST, GET(detailed), PUT and DELETE
    def _setup_add_record(self):
        _data = {"name": "50 Cent", "birth_date":"2005-02-13"}
        response = self.client.post(url_1, _data)
        data = json.loads(response.content)["data"]
        return ( response, _data, data )

And in test case where you want the functionality:

# Check the response if there is no given data
class ArtistTest(ArtistRecordSetupMixing, APITestCase):
    pass

Leave a comment