我对Django Rest Framework的测试有疑问。我有以下观点:
class AccountDetail(generics.RetrieveUpdateAPIView):
model = Account
serializer_class = AccountSerializer
lookup_field = 'username'
permission_classes = (permissions.AllowAny,)
def get_queryset(self):
return Account.objects.filter(username=self.kwargs.get('username'))
Run Code Online (Sandbox Code Playgroud)
我的网址包括以下内容:
account_urls = patterns('',
url(r'^/(?P<username>[0-9a-zA-Z_-]+)/$', AccountDetail.as_view(), name='account-detail'),
url(r'^/$', AccountList.as_view(), name='account-list')
)
urlpatterns = patterns('',
url(r'^api/v1/users', include(account_urls)),
)
Run Code Online (Sandbox Code Playgroud)
当我使用可浏览的API时,一切正常,但是当我编写以下测试时,出现错误:
class TestAccoundDetail(APITestCase):
def setUp(self):
factory = APIRequestFactory()
#set up normal user and token
self.normal_user = Account.objects.create_user(email="user@example.com", username="useri", password="man")
request = factory.post('/api/v1/auth/login/', {'email': 'user@example.com', 'password': 'man'})
response = obtain_jwt_token(request)
self.normal_token = response.data['token']
def test_details(self):
factory = APIRequestFactory()
view = AccountDetail.as_view()
#unauthenticated
url …Run Code Online (Sandbox Code Playgroud)