测试期间不遵守设置的默认语言 [英] Default language via settings not respected during testing

查看:19
本文介绍了测试期间不遵守设置的默认语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Django 1.3、Python 2.6

Using Django 1.3, Python 2.6

有一个特别奇怪的问题需要追踪国际化,以及 RequestFactory 与 TestClient 进行测试意见.

Having a particularly weird problem to track down related to internationalization, and RequestFactory vs. TestClient for testing views.

如果我跑:

./manage.py test 

所有测试都运行(包括有问题的测试)并成功通过.如果我跑:

All tests run (including the problematic ones) and pass successfully. If I run:

./manage.py test <appname> 

应用程序的测试将失败,引发模板渲染异常使用语言代码的模板,因为语言 django认为请求所要求的不是我们列出的语言设置.语言.(在这种情况下,它一直是 'en-us',我们支持的壁橱匹配语言是 'en')

The app's tests will fail, throwing a template rendering exception for templates that use the language code because the language django thinks the request is asking for is not a language we've listed in settings.LANGUAGES. (In this case it has always been 'en-us', the closet matching language we support being 'en')

下面是一个失败的测试示例:

Here's an example of a test that will fail:

class TemplateServingTestCase(TestCase):
    def setUp(self):
        self.app_dir      = os.path.abspath(os.path.dirname(__file__))
        self.gallery_root = os.path.join(self.app_dir, 'test_gallery')
        self.gallery_url  = '/'
        self.request      = RequestFactory().get('/')

    def test_404_invalid_category(self):
        self.assertRaises(Http404, gallery_page,
            self.request,
            'bad-category',
            self.gallery_root,
            self.gallery_url
        )

如果用django的TestClient做一个就不会出现这个问题了请求调用特定视图的 url.但是,如果相同视图只是用 RequestFactory 的 get 或 put 的结果调用方法,它会抛出上面的错误.

This problem will not occur if django's TestClient is used to make a request to a url that calls a particular view. However if that same view is simply called with the result of RequestFactory's get or put methods, it will throw the error above.

看起来好像在使用 RequestFactory 方法时,设置文件不被尊重.我错过了一些简单的东西吗在这里?

It appears as though when using the RequestFactory method, the settings file is not being respected. Am I missing something simple here?

适用的区域设置

LANGUAGE_CODE = 'en'
LANGUAGES = (
    ('en', 'English'),
    ('de', 'Deutsch'),
    ('es', 'Espanol'),
    ('fr', 'Francaise'),
    ('it', 'Italiano'),
    ('pt-br', 'Portugues (Brasil)'),
)

活动中间件

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'services.middleware.LegacyIntegrationMiddleware',
)

推荐答案

有两种可能:

a) 您在 settings.MIDDLEWARE_CLASSES 中有 'django.middleware.locale.LocaleMiddleware'.

a) You have 'django.middleware.locale.LocaleMiddleware' in settings.MIDDLEWARE_CLASSES.

在这种情况下,客户端使用settings.LANGUAGE_CODE.

In this case, the client use settings.LANGUAGE_CODE.

b) 你没有.

在这种情况下,你应该在你的 tests.py 模块中的某个地方设置这样的语言:

In that case, you should set the language like that somewhere in your tests.py module:

from django.utils.translation import activate
...
activate('fr-fr')

https://code.djangoproject.com/ticket/15143

这篇关于测试期间不遵守设置的默认语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆