Джанго Тестирование посмотреть шаблон контекста - PullRequest
0 голосов
/ 08 мая 2018

Я пытаюсь проверить

return render(request, 'template.html', context)

и, похоже, терпит неудачу. Не стоит ли это тестировать? Или, если стоит протестировать это, как мне это сделать?

view.py

def create_employee_profile(request):
    name_form = EmployeeNameForm()
    context = {'name_form':name_form}
    return render(request,
                'template_create_employee_profile.html',
                context
                )

Я знаю, что if: else: утверждения отсутствуют. Я не думаю, что они имеют отношение к тесту.

test.py

# TEST:  context({'name_form':name_form})
def test_CreateEmployeeProfileView_context(self):
    name_form = EmployeeNameForm()
    response = self.client.get(reverse(
                            'create_employee_profile'))
    self.assertEquals(response.context['name_form'], name_form)

Это дало мне самый близкий успех. Вот моя ошибка:

AssertionError: <Empl[27 chars]alid=False,
                fields=(employee_choices;first_nam[20 chars]ame)> != 
                <Empl[27 chars]alid=Unknown,
                fields=(employee_choices;first_n[22 chars]ame)>

А как насчет подробного вида?

# TEST:  context({'name':name})
def test_CustomerEmployeeProfileView_context(self):
    name = CustomerEmployeeName.objects.get(pk=1)
    response = self.client.get(
        reverse('service:customer_employee_profile_detail', kwargs={'pk': 1}))

    self.assertIsInstance(response.context['name'], name)

Получил эту ошибку:

TypeError: isinstance() arg 2 must be a type or tuple of types

1 Ответ

0 голосов
/ 08 мая 2018

Вы сравниваете два разных экземпляра EmployeeNameForm, поэтому утверждение не выполняется.

Если вы просто хотите проверить, что переменная контекста действительно является EmployeeNameForm, вы можете проверить это с помощью assertIsInstance :

def test_CreateEmployeeProfileView_context(self):
    response = self.client.get(reverse('create_employee_profile'))
    self.assertIsInstance(response.context['name_form'], EmployeeNameForm)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...