使用API​​TestCase和django-rest-framework

Aar*_*ier 9 python testing django rest django-rest-framework

我按照这个代码:

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)
Run Code Online (Sandbox Code Playgroud)

在django-rest-framewok docs中找到:

http://www.django-rest-framework.org/api-guide/testing/#example

Model用单个字段创建了一个单独的字段name,我仍然收到"错误请求400错误".视图和reverse名称也正确设置,我已手动测试成功查看URL.我没有启用身份验证

并且无法弄清楚我是否错过了一步?

有没有人有一个django-rest-framework APITestCase create model object测试代码片段的工作示例?

谢谢

Aar*_*ier 13

这个 GIT回购有几个工作的例子,我能够遵循并开始APITestCase工作:

https://github.com/erkarl/django-rest-framework-oauth2-provider-example/blob/master/apps/users/tests.py