Opened 12 years ago

Closed 12 years ago

Last modified 10 years ago

#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 hoffmaje, 12 years ago

Cc: hoffmaje added

comment:2 by budlight@…, 12 years ago

Keywords: MessageFailure added

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

'root': {

'level': 'WARNING',
'handlers': console,

},

and then run
./manage.py test messages

comment:3 by Claude Paroz, 12 years ago

Resolution: wontfix
Status: newclosed

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:

comment:4 by anonymous, 10 years ago

This makes the RequestFactory useless for my cases.

Note: See TracTickets for help on using tickets.
Back to Top