使用 Django 缓存中间件导致 contrib.auth 单元测试失败
问题:当我将 UpdateCacheMiddleware 和 FetchFromCacheMiddleware 添加到我的 Django 项目时,我遇到单元测试失败。这与我使用的 CACHE_BACKEND 无关(现在我使用的是 locmem://,但当我使用 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 ' 对象不可订阅
======================================================================
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
我一定是错过了一些东西(或者做错了一些事情),因为我在网上搜索了这个问题,但似乎没有人遇到过它。
重现步骤:
- 启动一个新的 django 项目 (django-admin.py startproject myproject) 并配置 settings.py
- 将 CACHE_BACKEND 添加到 settings.py 并从 Django 添加两个缓存中间件
- Run python manage.py test
注意:使用 dummy:// 缓存时只有一次测试失败,记录在:http://code.djangoproject.com/ticket/11640
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)
My Middleware:
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',
)
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.
Steps to Reproduce:
- Start a new django project (django-admin.py startproject myproject) and configure settings.py
- Add CACHE_BACKEND to settings.py and add the two Cache Middlewares from Django
- Run python manage.py test
Notes: There is only one test failure when using dummy:// cache and it is documented at: http://code.djangoproject.com/ticket/11640
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
失败测试的解决方案是将 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.