请帮助TestCase:test_edit_thing。
以下是错误详情:
' / things / django-book / edit /'的错误:
while(drawinSale != 1)
' / things / django-book /'
的错误# AssertionError: No templates used to render the response
response = self.client.get('/things/django-book/edit/')
self.assertTemplateUsed(response, 'things/edit_thing.html')
代码段:
# AssertionError: Template 'things/edit_thing.html' was not a template used to render the response. Actual template(s) used: things/thing_detail.html, base.html
response = self.client.get('/things/django-book/')
self.assertTemplateUsed(response, 'things/edit_thing.html')
from django.contrib.auth.models import AnonymousUser, User
from django.test import TestCase, RequestFactory
from collection.models import Thing
from .views import *
class SimpleTest(TestCase):
def setUp(self):
# Every test needs access to the request factory.
self.factory = RequestFactory()
self.user = User.objects.create_user(username='Foo', password='barbaz')
self.thing = Thing.objects.create(name="Django Book",
description="Learn how to build your first Django web app.",
slug="django-book",
user=self.user)
def test_index(self):
request = self.factory.get('/index/')
response = index(request)
print(response.status_code)
self.assertEqual(response.status_code, 200)
response = self.client.get('/')
self.assertTemplateUsed(response, 'base.html')
def test_thing_detail(self):
django_book = Thing.objects.get(name="Django Book")
request = self.factory.get('/things/django-book/')
response = thing_detail(request, django_book.slug)
print(response.status_code)
self.assertEqual(response.status_code, 200)
response = self.client.get('/things/django-book/')
self.assertTemplateUsed(response, 'things/thing_detail.html')
def test_edit_thing(self):
django_book = Thing.objects.get(name="Django Book")
request = self.factory.get('/things/django-book/edit/')
request.user = django_book.user
response = edit_thing(request, django_book.slug)
print(response.status_code)
self.assertEqual(response.status_code, 200)
# AssertionError: No templates used to render the response
#response = self.client.get('/things/django-book/edit/')
# AssertionError: Template 'things/edit_thing.html' was not a template used to render the response. Actual template(s) used: things/thing_detail.html, base.html
#response = self.client.get('/things/django-book/')
#self.assertTemplateUsed(response, 'things/edit_thing.html')
# Right?
response = self.client.get('/things/django-book/')
self.assertTemplateUsed(response, 'things/thing_detail.html')
由于
答案 0 :(得分:0)
所以您遇到的问题是您的用户未经过身份验证,并且如果用户与您提出的Http404用户不同,那么
它位于您的edit_thing
视图
if thing.user != request.user:
raise Http404
所以你需要进行身份验证。这可以通过Layered Architecture Samples for .NET
进行def test_edit_thing(self):
django_book = Thing.objects.get(name="Django Book")
request = self.factory.get('/things/django-book/edit/')
request.user = django_book.user
response = edit_thing(request, django_book.slug)
print(response.status_code)
self.assertEqual(response.status_code, 200)
self.client.login(username='Foo', password='barbaz')
response = self.client.get('/things/django-book/edit/')
self.assertTemplateUsed(response, 'things/edit_thing.html')