Я пытаюсь проверить свой пользовательский метод get_context_data () в ListView, и я продолжаю сталкиваться с этой ошибкой: AttributeError: 'Home' object has no attribute 'object_list'
Представление работает нормально. Проверка того, что представление отвечает 200 кодом состояния, работает нормально. Я просто не могу проверить мой собственный метод get_context_data.
Тест
import pytest
from mixer.backend.django import mixer
from django.test import RequestFactory
from django.urls import reverse
from tracker.views import *
from tracker.models import Peak
pytestmark = pytest.mark.django_db
@pytest.fixture
def factory():
return RequestFactory()
def test_number_of_peaks_completed(factory):
mixer.cycle(7).blend(Peak, complete=True)
mixer.cycle(10).blend(Peak)
path = reverse('home')
request = factory.get(path)
view = Home()
view.setup(request)
context = view.get_context_data()
assert context['number_of_peaks_completed'] == 7
Просмотр
from django.views.generic import ListView
from tracker.models import *
class Home(ListView):
model = Peak
template_name = 'tracker/home.html'
context_object_name = 'peaks'
def get_context_data(self, *, object_list=None, **kwargs):
context = super().get_context_data(**kwargs)
context['number_of_peaks_completed'] = Peak.objects.filter(complete=True).count()
return context
Трассировка ошибок
FAILED [ 16%]
tracker/tests/test_views.py:24 (test_number_of_peaks_completed)
factory = <django.test.client.RequestFactory object at 0x7efbfb8093a0>
def test_number_of_peaks_completed(factory):
mixer.cycle(7).blend(Peak, completed=True)
mixer.cycle(10).blend(Peak, completed=False)
path = reverse('home')
request = factory.get(path)
view = Home()
view.setup(request)
> context = view.get_context_data()
tracker/tests/test_views.py:33:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
tracker/views.py:19: in get_context_data
context = super().get_context_data(**kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <tracker.views.Home object at 0x7efbfb630d00>, object_list = None
kwargs = {}
def get_context_data(self, *, object_list=None, **kwargs):
"""Get the context for this view."""
> queryset = object_list if object_list is not None else self.object_list
E AttributeError: 'Home' object has no attribute 'object_list'
venv/lib/python3.8/site-packages/django/views/generic/list.py:115: AttributeError