概述
我需要传递请求对象,并且请求对象需要启用消息传递中间件,因为在该功能中使用消息传递中间件.
我正在使用RequestFactory来创建我的请求. documentation说:
It does not support middleware. Session and authentication attributes must be supplied by the test itself if required for the view to function properly.
如何使用RequestFactory设置邮件中间件?我想我也需要会话中间件来使消息中间件工作
这是我当前使用香草RequestFactory时测试的错误.
MessageFailure: You cannot add messages without installing django.contrib.messages.middleware.MessageMiddleware
这是我正在测试的功能,以防它有助于理解这个问题:
from django.contrib import messages as django_messages def store_to_request(self,request): """ Place all the messages stored in this class into message storage in the request object supplied. :param request: The request object to which we should store all the messages :return: Does not return anything """ for message in self._messages: django_messages.add_message(request,message.level,message.message,message.extra_tags,message.fail_silently)
from django.contrib.messages.storage.fallback import FallbackStorage setattr(request,'session','session') messages = FallbackStorage(request) setattr(request,'_messages',messages)
总结
以上是编程之家为你收集整理的python – 如何在单元测试期间在Django RequestFactory中设置消息传递和会话中间件全部内容,希望文章能够帮你解决python – 如何在单元测试期间在Django RequestFactory中设置消息传递和会话中间件所遇到的程序开发问题。
如果您也喜欢它,动动您的小指点个赞吧