Fixed #20609 -- Added instructions for using AnonymousUser with RequestFactory.
This commit is contained in:
parent
968510e5d7
commit
a39df37049
|
@ -37,7 +37,7 @@ Example
|
||||||
|
|
||||||
The following is a simple unit test using the request factory::
|
The following is a simple unit test using the request factory::
|
||||||
|
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import AnonymousUser, User
|
||||||
from django.test import TestCase, RequestFactory
|
from django.test import TestCase, RequestFactory
|
||||||
|
|
||||||
class SimpleTest(TestCase):
|
class SimpleTest(TestCase):
|
||||||
|
@ -55,6 +55,10 @@ The following is a simple unit test using the request factory::
|
||||||
# logged-in user by setting request.user manually.
|
# logged-in user by setting request.user manually.
|
||||||
request.user = self.user
|
request.user = self.user
|
||||||
|
|
||||||
|
# Or you can simulate an anonymous user by setting request.user to
|
||||||
|
# an AnonymousUser instance.
|
||||||
|
request.user = AnonymousUser()
|
||||||
|
|
||||||
# Test my_view() as if it were deployed at /customer/details
|
# Test my_view() as if it were deployed at /customer/details
|
||||||
response = my_view(request)
|
response = my_view(request)
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
Loading…
Reference in New Issue