views:

305

answers:

1

In my django application, I'm trying to write a unit test that performs an action and then checks the messages in the response.

As far as I can tell, there is no nice way of doing this.

I'm using the CookieStorage storage method, and I'd like to do something similar to the following:

    response = self.client.post('/do-something/', follow=True)
    self.assertEquals(response.context['messages'][0], "fail.")

The problem is, all I get back is a

print response.context['messages']
<django.contrib.messages.storage.cookie.CookieStorage object at 0x3c55250>

How can I turn this into something useful, or am I doing it all wrong?

Thanks, Daniel

+1  A: 

I did an experiment to test this. I changed the MESSAGE_STORAGE setting in one of my projects to 'django.contrib.messages.storage.cookie.CookieStorage' and executed a test that I had written to check for messages. It worked.

The key difference from what you were doing is the way I retrieved messages. See below:

def test_message_sending(self):
    data = dict(...)
    response = self.client.post(reverse('my_view'), data)
    messages = self.user.get_and_delete_messages()

    self.assertTrue(messages)
    self.assertEqual('Hey there!', messages[0])

This may be worth a try.

Manoj Govindan