#17971 closed Bug (wontfix)
RequestFactory requests can't be used to test views that call messages.add
Reported by: | hoffmaje | Owned by: | nobody |
---|---|---|---|
Component: | Testing framework | Version: | 1.4 |
Severity: | Normal | Keywords: | RequestFactory, MessageFailure |
Cc: | hoffmaje | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
To reproduce the problem:
- start new project with a new app
- define a view 'my_view' that calls messages.add
- define a test for that view as follows:
def test_my_view(self): request = RequestFactory().get('/my_view/') my_view(request)
Running the test outputs:
File "/usr/local/lib/python2.7/dist-packages/django/contrib/messages/api.py", line 22, in add_message raise MessageFailure('You cannot add messages without installing ' MessageFailure: You cannot add messages without installing django.contrib.messag es.middleware.MessageMiddleware
Note: The same test passed in 1.3.
I could fix my tests, by setting:
setattr(request, 'session', 'session') messages = FallbackStorage(request) setattr(request, '_messages', messages)
Change History (4)
comment:1 by , 13 years ago
Cc: | added |
---|
comment:2 by , 13 years ago
Keywords: | MessageFailure added |
---|
comment:3 by , 13 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
I think this is by design. Quote from the 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.
So either you can:
- provide the missing messages data to the request (simulating the message middleware)
- use the full test client which fully supports middlewares.
- add the fail_silently parameter to the add_message call in your view (https://docs.djangoproject.com/en/dev/ref/contrib/messages/#failing-silently-when-the-message-framework-is-disabled)
Note:
See TracTickets
for help on using tickets.
This isn't actually a test failure, but you get this exact same message in a default install of django 1.4 as soon as you enable the root logger with
and then run
./manage.py test messages