Django учебная часть 5. При тестировании Indexview не появляется сообщение «Нет доступных опросов». - PullRequest
0 голосов
/ 13 февраля 2020

Я попытался запустить тестирование на django учебной части 5. Но я не смог пройти.

Класс тестирования похож на ниже

class QuestionIndexViewTests(TestCase):

def test_no_questions(self):
    """
    If no questions exists, an appropriate message is displayed
    """

    response = self.client.get(reverse('polls:index'))
    self.assertEqual(response.status_code, 200)
    self.assertContains(response, "No polls are available.")
    self.assertQuerysetEqual(response.context['latest_question_list'], [])

def test_future_question(self):
    """
    Questions with a pub_date in the future aren't displayed on
    the index page.
    """
    create_question(question_text="Future question.", days=30)
    response = self.client.get(reverse('polls:index'))
    self.assertContains(response, "No polls are available.")
    self.assertQuerysetEqual(response.context['latest_question_list'], [])

def test_past_question(self):
    """
    Questions with pub_date in the past are displayed on the index page
    """

    create_question(question_text="Past question.", days=-30)
    response = self.client.get(reverse('polls:index'))
    self.assertQuerysetEqual(
        response.context['latest_question_list'],
        ['<Question: Past question.>']
    )

def test_future_question_and_past_question(self):
    """
    Even if both past and future questions exist, only past questions
    are displayed
    """

    create_question(question_text="Past question.", days=-30)
    create_question(question_text="Future question.", days=30)
    response = self.client.get(reverse('polls:index'))
    self.assertQuerysetEqual(
        response.context['latest_question_list'],
        ['<Question: Past question.>']
    )

def test_two_past_questions(self):
    """
    The questions index page may display multiple questions.
    """
    create_question(question_text="Past question 1.", days=-30)
    create_question(question_text="Past question 2.", days=-5)
    response = self.client.get(reverse('polls:index'))
    self.assertQuerysetEqual(
        response.context['latest_question_list'],
        ['<Question: Past question 2.>', '<Question: Past question 1.>']
    )

Произошли ошибки в функциях test_future_question и test_future_question_and_past_question. Я смог пройти тест, когда закомментировал операторы self.assertContains (ответ: «Нет доступных опросов».) «Мне кажется, что мне нужно пропустить сообщение из поля зрения, когда вопросов нет. Но я понятия не имею, как передать сообщение. Мой класс просмотра, как показано ниже.

class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'

def get_queryset(self):
    """
    Return the last five published questions (not including those set to be
    published in the future)
    """
    return Question.objects.filter(pub_date__lte=timezone.now()).order_by('-pub_date')[:5]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...