Reputation: 20810
I followed this code:
from django.core.urlresolvers import reverse
from rest_framework import status
from rest_framework.test import APITestCase
class AccountTests(APITestCase):
def test_create_account(self):
"""
Ensure we can create a new account object.
"""
url = reverse('account-list')
data = {'name': 'DabApps'}
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(response.data, data)
Found in the django-rest-framewok docs here:
DRF API-guide: Testing example
I created a single Model
with a single field name
, and I am still getting a "bad request 400 error". The view and reverse
name is also set up correctly, and I have manually tested viewing the URL with success. I don't have Authentication enabled
And can't figure out if I am missing a step?
Does anyone have a working example of a django-rest-framework APITestCase create model object
test code snippet?
Upvotes: 18
Views: 26873
Reputation: 623
It could be a JSON decode error.
In the line self.client.post(url, data, format='json')
use json.dumps(data)
and try.
Upvotes: 1
Reputation: 20810
This GIT
repo has several working examples, which I was able to follow and get APITestCase
working:
django-rest-framework-oauth2-provider-example/apps/users/tests.py
Upvotes: 22