5.1 Testing the DetailView

master
Jason Zhu 2020-10-20 22:53:38 +11:00
parent 447c1a49e5
commit d6058c5210
3 changed files with 55 additions and 2 deletions

View File

@ -544,7 +544,7 @@ Django provides a test **Client** (a class) to simulate a user interacting with
We can try it in interactive shell following the guide We can try it in interactive shell following the guide
#### Improving our view #### Test and Improve our view
Add following in `tests.py` Add following in `tests.py`
@ -665,3 +665,29 @@ Destroying test database for alias 'default'...
Fix it by change `get_queryset()` method in Question. 让他它能通过将 Question 的 pub_data 属性与 timezone.now() 相比较来判断是否应该显示此 Question Fix it by change `get_queryset()` method in Question. 让他它能通过将 Question 的 pub_data 属性与 timezone.now() 相比较来判断是否应该显示此 Question
#### Testing the DetailView
Target: hide future questions even if user type correct URL
Add test cases first
```python
class QuestionDetailViewTests(TestCase):
def test_future_question(self):
"""
The detail view of a question with a pub_date in the future returns a 404 not found.
"""
future_question = create_question(question_text="Future question.", days=5)
url = reverse('poll:detail', args=(future_question.id, ))
response = self.client.get(url)
self.assertEqual(response.status_code, 404)
def test_past_questioin(self):
"""
The detail view of a question with a pub_date in the past displays the question's text.
"""
past_question = create_question(question_text='Past Question.', days=-5)
url = reverse('polls:detail', args=(past_question.id,))
response = self.client.get(url)
self.assertContains(response, past_question.question_text)
```

View File

@ -1,4 +1,6 @@
import datetime import datetime
from django.conf.urls import url
from django.http import response
from django.test import TestCase from django.test import TestCase
from django.utils import timezone from django.utils import timezone
@ -101,3 +103,22 @@ class QuestionIndexViewTests(TestCase):
response.context['latest_question_list'], response.context['latest_question_list'],
['<Question: Past question 2.>', '<Question: Past question 1.>'] ['<Question: Past question 2.>', '<Question: Past question 1.>']
) )
class QuestionDetailViewTests(TestCase):
def test_future_question(self):
"""
The detail view of a question with a pub_date in the future returns a 404 not found.
"""
future_question = create_question(question_text="Future question.", days=5)
url = reverse('polls:detail', args=(future_question.id, ))
response = self.client.get(url)
self.assertEqual(response.status_code, 404)
def test_past_questioin(self):
"""
The detail view of a question with a pub_date in the past displays the question's text.
"""
past_question = create_question(question_text='Past Question.', days=-5)
url = reverse('polls:detail', args=(past_question.id,))
response = self.client.get(url)
self.assertContains(response, past_question.question_text)

View File

@ -24,6 +24,12 @@ class DetailView(generic.DetailView):
model = Question model = Question
template_name = 'polls/detail.html' template_name = 'polls/detail.html'
def get_queryset(self):
"""
Excludes any questions that aren't published yet
"""
return Question.objects.filter(pub_date__lte=timezone.now())
class ResultsView(generic.DetailView): class ResultsView(generic.DetailView):
model = Question model = Question
template_name = 'polls/results.html' template_name = 'polls/results.html'