使用Django Cache Middleware会导致contrib.auth单元测试失败 [英] Using Django Cache Middleware causes contrib.auth unit tests to fail
问题描述
问题::将UpdateCacheMiddleware和FetchFromCacheMiddleware添加到我的Django项目中时,出现单元测试失败.这与我使用的CACHE_BACKEND无关(现在我正在使用locmem://,但是当我使用file:///path_to_cache时,错误是相同的)
Problem: When I add UpdateCacheMiddleware and FetchFromCacheMiddleware to my Django project, I get unittest failures. This is regardless of the CACHE_BACKEND I use (right now I am using locmem://, but the errors are the same when I use file:///path_to_cache)
我的中间件:
MIDDLEWARE_CLASSES = (
'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
)
我所有的测试失败看起来都像下面这样:'NoneType'对象不可取消
All my test failures look like the one below: 'NoneType' object is unsubscriptable
======================================================================
Error: test_last_login (django.contrib.auth.tests.remote_user.RemoteUserTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python26\lib\site-packages\django\contrib\auth\tests\remote_user.py",
line 87, in test_last_login
self.assertNotEqual(default_login, response.context['user'].last_login)
TypeError: 'NoneType' object is unsubscriptable
在网络上搜索此问题时,我一定会丢失某些东西(或做错了什么),但似乎没人遇到过.
I must be missing something (or doing something wrong) as I have searched around the web for this issue, but no one seems to have encountered it.
复制步骤:
- 启动一个新的django项目(django-admin.py startproject myproject)并配置settings.py
- 将CACHE_BACKEND添加到settings.py,并从Django添加两个缓存中间件
- 运行python manage.py测试
注意事项:使用dummy://缓存时只有一次测试失败,并记录在以下位置: http://code.djangoproject.com/ticket/11640
Notes: There is only one test failure when using dummy:// cache and it is documented at: http://code.djangoproject.com/ticket/11640
推荐答案
对于失败的测试,解决方案是将CACHE_MIDDLEWARE_SECONDS设置为0(例如,在您的开发环境中将其设置为0).这将使django.contrib测试全部通过.
The solution to the failing tests is to set CACHE_MIDDLEWARE_SECONDS to 0 (e.g. set this to be 0 in your dev environment). This will allow the django.contrib tests to all pass.
这篇关于使用Django Cache Middleware会导致contrib.auth单元测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!