Fixed #16087 -- Added ResolverMatch instance to test client response.

Thanks mrmachine for the suggestion.
This commit is contained in:
Greg Chapple 2014-06-06 15:49:02 +01:00 committed by Tim Graham
parent 2d425116e2
commit bf743a4d57
5 changed files with 54 additions and 4 deletions

View file

@ -214,8 +214,11 @@ Tests
* The new :meth:`~django.test.SimpleTestCase.assertJSONNotEqual` assertion
allows you to test that two JSON fragments are not equal.
* Added the ability to preserve the test database by adding the :djadminopt:`--keepdb`
flag.
* Added the ability to preserve the test database by adding the
:djadminopt:`--keepdb` flag.
* Added the :attr:`~django.test.Response.resolver_match` attribute to test
client responses.
Validators
^^^^^^^^^^

View file

@ -432,6 +432,25 @@ Specifically, a ``Response`` object has the following attributes:
loaded from a file. (The name is a string such as
``'admin/index.html'``.)
.. attribute:: resolver_match
.. versionadded:: 1.8
An instance of :class:`~django.core.urlresolvers.ResolverMatch` for the
response. You can use the
:attr:`~django.core.urlresolvers.ResolverMatch.func` attribute, for
example, to verify the view that served the response::
# my_view here is a function based view
self.assertEqual(response.resolver_match.func, my_view)
# class based views need to be compared by name, as the functions
# generated by as_view() won't be equal
self.assertEqual(response.resolver_match.func.__name__, MyView.as_view().__name__)
If the given URL is not found, accessing this attribute will raise a
:exc:`~django.core.urlresolvers.Resolver404` exception.
You can also use dictionary syntax on the response object to query the value
of any settings in the HTTP headers. For example, you could determine the
content type of a response using ``response['Content-Type']``.