How to access the contents of a Django message structure in Django unit tests

Using the Django message structure, I pass messages to a template for visualization in various scenarios - creating a user account successfully, etc. The message is stored in the cookie for the session:

print response.cookies['messages'] Set-Cookie: messages="b6870b4797b65640bb535519a5b53808fdc0ea24$[[\"__json_message\"\05420\054\"Account verified\054 you are now logged in\"]]"; Path=/ 

The cookie is a Morsel object, but I can't seem to pull out its components to test the contents of the message. Any help would be greatly appreciated!

+6
source share
1 answer

Edit: 10-05-2014:

An alternative method is to repeat messages in the context of the response. Using the Django test client, response message elements can be parsed using:

 for message in response.context['messages']: 

Returns each Django Message object, then you can query the attributes for your tests. This is a cleaner alternative to the original option.

Original solution:

For archiving purposes, the original working solution was to interrogate cookies in response files. This is less clean than the new solution.

 self.assertTrue('Account verified' in response.cookies['messages'].value) 

in unittest. This seems like a rather ugly solution, but since there will be no other โ€œVerified accountโ€ or other simultaneous message, then it will be acceptable.

+8
source

Source: https://habr.com/ru/post/907941/


All Articles