0
私は自分の意見にtests.pyを書き込もうとします。どのように私はユニークなスラッグでDjangoのビューをテストする必要があります
私は、スラッグで見るには良いテストを書くことに問題があります。
views.py:
def post_detail(request, slug):
post = Post.objects.get(slug=slug)
context = {'post': post}
template = 'post/post_detail.html'
return render(request, template, context)
URLは次のようになります。ポスト/ slug_name、およびスラグは、すべてのポストのためにユニークです。
私が試してみてください。
tests.py
class PostDetailTestCase(TestCase):
def test_post_detail_(self):
test = Post(title='poost', description="posting", category="letstest", slug='django')
response = self.client.get(reverse('board_detail', args=(self.slug,)))
self.assertEqual(response.status_code, 200)
ラン・テスト・エラー:私はそれを修復する必要がありますどのように
response = self.client.get(reverse('post_detail', args=(self.slug,
AttributeError: 'PostDetailTestCase' object has no attribute 'slug'
を?前もって感謝します。
メイト、ありがとうございます。それは単純で美しかったです。 "蘭のテスト - OK"。 .objectsを忘れてしまった。 –