[英]using APITestCase with django-rest-framework
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: 在django-rest-framewok docs中找到:
http://www.django-rest-framework.org/api-guide/testing/#example http://www.django-rest-framework.org/api-guide/testing/#example
I created a single Model
with a single field name
, and I am still getting a "bad request 400 error". 我创建了一个带有单个字段
name
Model
,我仍然收到“错误请求400错误”。 The view and reverse
name is also set up correctly, and I have manually tested viewing the URL with success. 视图和
reverse
名称也正确设置,我已手动测试成功查看URL。 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? 有没有人有一个
django-rest-framework APITestCase create model object
测试代码片段的工作示例?
Thanks 谢谢
This GIT
repo has several working examples, which I was able to follow and get APITestCase
working: 这个
GIT
repo有几个工作示例,我能够遵循并让APITestCase
工作:
https://github.com/erkarl/django-rest-framework-oauth2-provider-example/blob/master/apps/users/tests.py https://github.com/erkarl/django-rest-framework-oauth2-provider-example/blob/master/apps/users/tests.py
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.