简体   繁体   English

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

[英]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.

相关问题 使用Django Rest Framework的APITestCase时出错 - Error when using Django Rest Framework's APITestCase 使用django-rest-framework进行密码哈希处理? - Password hash using django-rest-framework? 如何使用django-rest-framework序列化ValuesQuerySet? - How to serialize a ValuesQuerySet using django-rest-framework? 使用 django-rest-framework 自动“由用户创建”字段? - Automatic 'created by user' field using django-rest-framework? 使用分页模块的 Django-Rest-Framework csv 模块 - Django-Rest-Framework csv module using paginated module 我无法使用 Django-Rest-Framework 注册用户 - I cannot register a user using Django-Rest-Framework Django Rest Framework中APITestCase,APISImpleTestCase和APITransactionTestCase的不同用例有哪些? - What are the different use cases for APITestCase, APISImpleTestCase, and APITransactionTestCase in Django Rest Framework 在django-rest-framework的过滤器中使用自定义方法 - Using custom methods in filter with django-rest-framework 使用视图的django-rest-framework通过POST返回500 - django-rest-framework using views returns 500 with a POST 使用django-rest-framework将模型对象序列化为JSON dict - Serialize model objects into a JSON dict using django-rest-framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM