Why don’t my Django unittests know that MessageMiddleware is installed?

Django 1.4 has a expected behavior when you create the request with RequestFactory that can trigger this error.

To resolve this issue, create your request with RequestFactory and do this:

from django.contrib.messages.storage.fallback import FallbackStorage
setattr(request, 'session', 'session')
messages = FallbackStorage(request)
setattr(request, '_messages', messages)

Works for me!

Leave a Comment