I am trying to make a test pass with a mocked function:
@mock.patch('apps.myapp.views.messages')
def test_message_get_called(self, messages):
# tests
assert messages.add_message.called
This tests pass perfectly by default, no problem here.
Because I have a long views.py
file I try to refactor. Instead of having a single file, I create a folder named views
, put my views.py
inside as all.py
and add the following __init__.py
:
from __future__ import absolute_import
from .all import (
....
)
So I import all my views class into this file to have access to them. This make the application work without problem, so I'm guessing the tests should be fine.
But it turns out the test fails:
AttributeError: <module 'apps.myapp.views' from '.../views/__init__.pyc'>
does not have the attribute 'messages'
So I add messages
in my list of import. And I get the following error:
MessageFailure: You cannot add messages without installing
django.contrib.messages.middleware.MessageMiddleware
I already found that using middleware during test may cause problem:
- https://code.djangoproject.com/ticket/17971
- Why don't my Django unittests know that MessageMiddleware is installed?
But my tests seems to pass without the module, and it fails with.
What can be the issue here ?