在我的每个视图中,我都有一个返回浏览器请求语言代码的函数。但是当我运行我的单元测试时,我的视图出现了错误:
applications\emplois\views.py:74:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <applications.emplois.views.IndexView object at 0x0000000006270048>
def language(self):
"""Return the user default language"""
> language = language_set(self.request.LANGUAGE_CODE)
E AttributeError: 'WSGIRequest' object has no attribute 'LANGUAGE_CODE'
applications\emplois\views.py:64: AttributeError
---------------------------- Captured stdout setup ----------------------------
Creating test database for alias 'default'...
=================== 1 failed, 11 deselected in 5.97 seconds ===================
视图.py
def language_set(language):
if "-" in language:
return (language.split('-')[1]).upper()
else:
return language.upper()
class IndexView(generic.ListView):
"""
this is the ROOT page
return a list of Jobs
"""
template_name='emplois/index.html'
context_object_name='latest_jobs_list'
paginate_by = 10
def language(self):
"""Return the user default language"""
language = language_set(self.request.LANGUAGE_CODE)
language = language.upper()
return language
def get_queryset(self):
"""
Return a list of Jobs that have an EXPIRATION DATE
greater than Now() and a default Language
"""
#import ipdb;ipdb.set_trace()
return Job.objects.filter(language=self.language(),\
EXPIRYDATE__gt=datetime.now())\
.order_by('EXPIRYDATE')
测试:
import pytest
from django.core import mail
from django.contrib.auth.models import AnonymousUser
from django.http import Http404
from django.test import RequestFactory
from mock import patch
from mixer.backend.django import mixer
pytestmark = pytest.mark.django_db
from applications.emplois import views
class TestIndexView:
def test_anonymous(self):
req = RequestFactory().get('/')
resp = views.IndexView.as_view()(req)
assert resp.status_code == 200, 'Should be callable by anyone'
由于我不使用浏览器,如何绕过语言请求