Opened 3 years ago

Closed 3 years ago

Last modified 17 months 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 Changed 3 years ago by hoffmaje

  • Cc hoffmaje added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 3 years ago by budlight@…

  • 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 Changed 3 years ago by claudep

  • Resolution set to wontfix
  • Status changed from new to 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:

comment:4 Changed 17 months ago by anonymous

This makes the RequestFactory useless for my cases.

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