我一直在努力通過Django tutorial,我在第5部分,它要求我創建一個測試,以查看沒有選擇的民意調查是否正在發佈。對於每一個其他測試我們所做的,就像過去確保只有民意調查(而不是將來)公佈,我們將簡單地創建兩個民意調查:一個在過去pub_date
,一個在未來:防止沒有選擇的民意調查發佈
def test_index_view_with_future_poll_and_past_poll(self):
"""
Even if both past and future polls exist, only past polls should be
displayed.
"""
create_poll(question='past poll', days=-30)
create_poll(question='future poll', days=30)
response = self.client.get(reverse('polls:index'))
self.assertQuerysetEqual(
response.context['latest_poll_list'],
['<Poll: past poll>'])
對於相應的視圖,我們只是增加了以下功能:
def get_queryset(self):
"""Return the last five published poll."""
#Returns polls published at a date the same as now or before
return Poll.objects.filter(pub_date__lte=timezone.now()).order_by('pub_date')[:5]
它只是使用的過濾器()函數來過濾掉在將來,這其實很簡單一個pub_date
任何調查。但我似乎無法對沒有任何選擇投票做的一樣,這是我迄今爲止的測試功能:
class PollsAndChoices(TestCase):
""" A poll without choices should not be displayed
"""
def test_poll_without_choices(self):
#first make an empty poll to use as a test
empty_poll = create_poll(question='Empty poll', days=-1)
poll = create_poll(question='Full poll',days=-1)
full_poll = poll.choice_set.create(choice_text='Why yes it is!', votes=0)
#create a response object to simulate someone using the site
response = self.client.get(reverse('polls:index'))
self.assertQuerysetEqual(response.context['latest_poll_list'], ['<Poll: Full poll>'])
而這就是我對相關觀點:
class IndexView(generic.ListView):
#We tell ListView to use our specific template instead of the default just like the rest
template_name = 'polls/index.html'
#We use this variable because the default variable for Django is poll_list
context_object_name = 'latest_poll_list'
def get_queryset(self):
"""Return the last five published poll."""
#Returns polls published at a date the same as now or before
return Poll.objects.filter(pub_date__lte=timezone.now()).order_by('pub_date')[:5]
def show_polls_with_choices(self):
""" Only publish polls that have choices """
#Since choice_set displays a list we can use it to make sure a poll with an empty list
#is not published
for poll in Poll.objects.all():
if poll.choice_set.all() is not []:
return poll
基本上什麼也沒發生,測試失敗:
Traceback (most recent call last):
File "C:\Users\ELITEBOOK\dropbox\programming\mysite\polls\tests.py", line 125, in test_poll_without_choices
self.assertQuerysetEqual(response.context['latest_poll_list'], ['<Poll: Full poll>'])
File "C:\Users\Elitebook\Dropbox\Programming\virtualenvs\project\lib\site- packages\django\test\testcases.py", line 829
, in assertQuerysetEqual
return self.assertEqual(list(items), values)
AssertionError: Lists differ: ['<Poll: Empty poll>', '<Poll:... != ['<Poll: Full poll>']
First differing element 0:
<Poll: Empty poll>
<Poll: Full poll>
First list contains 1 additional elements.
First extra element 1:
<Poll: Full poll>
- ['<Poll: Empty poll>', '<Poll: Full poll>']
+ ['<Poll: Full poll>']
所以應用仍在出版空的投票和充分調查兩者是否有使用filter()
方法REF方式基於他們是否有選擇進行民意測驗?
HTTP運行測試至少創建一個選擇的get_queryset:// stackoverflow.com/a/844572/3033586 – madzohan 2014-11-09 11:25:48