如何测试新的 Django 1.10+ 中间件 类
How to test new Django 1.10+ MIDDLEWARE classes
我正在将现有的中间件升级到新样式的 1.10+ Django 中间件。
之前是这样的:
class ThreadLocalMiddleware(MiddlewareMixin):
""" Simple middleware that adds the request object in thread local storage."""
_thread_locals = local()
def process_request(self, request):
_thread_locals.request = request
def process_response(self, request, response):
if hasattr(_thread_locals, 'request'):
del _thread_locals.request
return response
def process_exception(self, request, exception):
if hasattr(_thread_locals, 'request'):
del _thread_locals.request
改写为新样式后:
class ThreadLocalMiddleware:
_thread_locals = local()
def __init__(self, get_response=None):
self.get_response = get_response
def __call__(self, request):
_thread_locals.request = request
response = self.get_response(request)
if hasattr(_thread_locals, 'request'):
del _thread_locals.request
return response
def process_exception(self, request, exception):
if hasattr(_thread_locals, 'request'):
del _thread_locals.request
我的问题是如何对中间件在_thread_locals
中设置request
进行单元测试?以前它就像在我的单元测试中调用 process_request
方法一样简单。现在我只有 __call__
方法,它会在最后擦除变量。
看django的中间件测试,他们仍然使用向后兼容class允许他们保留旧的测试,我想知道它被删除后他们将如何测试它。
最后我用了一个类似的测试。 get_response_callback
在中间件的预请求工作完成后被调用。
def test_middleware():
def get_response_callback(req):
assert ThreadLocalMiddleware._thread_locals.request
middleware = ThreadLocalMiddleware(get_response_callback)
request = RequestFactory().get('/')
request.user = AnonymousUser()
middleware(request)
assert hasattr(ThreadLocalMiddleware._thread_locals, 'request') == False
我正在将现有的中间件升级到新样式的 1.10+ Django 中间件。
之前是这样的:
class ThreadLocalMiddleware(MiddlewareMixin):
""" Simple middleware that adds the request object in thread local storage."""
_thread_locals = local()
def process_request(self, request):
_thread_locals.request = request
def process_response(self, request, response):
if hasattr(_thread_locals, 'request'):
del _thread_locals.request
return response
def process_exception(self, request, exception):
if hasattr(_thread_locals, 'request'):
del _thread_locals.request
改写为新样式后:
class ThreadLocalMiddleware:
_thread_locals = local()
def __init__(self, get_response=None):
self.get_response = get_response
def __call__(self, request):
_thread_locals.request = request
response = self.get_response(request)
if hasattr(_thread_locals, 'request'):
del _thread_locals.request
return response
def process_exception(self, request, exception):
if hasattr(_thread_locals, 'request'):
del _thread_locals.request
我的问题是如何对中间件在_thread_locals
中设置request
进行单元测试?以前它就像在我的单元测试中调用 process_request
方法一样简单。现在我只有 __call__
方法,它会在最后擦除变量。
看django的中间件测试,他们仍然使用向后兼容class允许他们保留旧的测试,我想知道它被删除后他们将如何测试它。
最后我用了一个类似的测试。 get_response_callback
在中间件的预请求工作完成后被调用。
def test_middleware():
def get_response_callback(req):
assert ThreadLocalMiddleware._thread_locals.request
middleware = ThreadLocalMiddleware(get_response_callback)
request = RequestFactory().get('/')
request.user = AnonymousUser()
middleware(request)
assert hasattr(ThreadLocalMiddleware._thread_locals, 'request') == False