2015-01-29 31 views
6

Tôi đi theo mã này:sử dụng APITestCase với django-nghỉ ngơi-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) 

Tìm thấy trong các tài liệu django-nghỉ ngơi-framewok đây:

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

Tôi tạo ra một đơn Model với một trường đơn name và tôi vẫn nhận được lỗi "yêu cầu 400 lỗi". Chế độ xem và tên reverse cũng được thiết lập chính xác và tôi đã thử nghiệm thủ công xem URL thành công. Tôi chưa bật Xác thực

Và không thể tìm hiểu xem tôi có thiếu một bước không?

Có ai có ví dụ làm việc về đoạn mã kiểm tra django-rest-framework APITestCase create model object không?

Cảm ơn

+0

Các bạn đã thử in 'response.errors' hoặc 'response.data '? – meshy

+0

@meshy, tôi đã kết thúc chỉ sau git repo dưới đây để làm cho nó hoạt động. Cảm ơn. –

Trả lời