summaryrefslogtreecommitdiff
path: root/docs/topics
diff options
context:
space:
mode:
authorSusanTan <onceuponatimeforever@gmail.com>2013-07-02 01:19:44 -0700
committerTim Graham <timograham@gmail.com>2013-07-03 11:12:04 -0400
commit583f340d7dd52c6182530fd4ca798d01c4d93609 (patch)
tree4ff2f1a3d7b2241c55ef6ba9a6a8976a30b1f34d /docs/topics
parent44df41e5fd151378aee2730c497f82e0db68666a (diff)
[1.5.x] Fixed #20609 -- Documented how to use request.user with RequestFactory
Thanks michel@ for the suggestion. Backport of 75041d5ea3 from stable/1.5.x.
Diffstat (limited to 'docs/topics')
-rw-r--r--docs/topics/testing/advanced.txt11
1 files changed, 9 insertions, 2 deletions
diff --git a/docs/topics/testing/advanced.txt b/docs/topics/testing/advanced.txt
index acbbd67d0c..c07b718a4d 100644
--- a/docs/topics/testing/advanced.txt
+++ b/docs/topics/testing/advanced.txt
@@ -37,18 +37,25 @@ Example
The following is a simple unit test using the request factory::
- from django.utils import unittest
+ from django.contrib.auth.models import User
+ from django.test import TestCase
from django.test.client import RequestFactory
- class SimpleTest(unittest.TestCase):
+ class SimpleTest(TestCase):
def setUp(self):
# Every test needs access to the request factory.
self.factory = RequestFactory()
+ self.user = User.objects.create_user(
+ first_name='jacob', email='jacob@…', password='top_secret')
def test_details(self):
# Create an instance of a GET request.
request = self.factory.get('/customer/details')
+ # Recall that middleware are not suported. You can simulate a
+ # logged-in user by setting request.user manually.
+ request.user = self.user
+
# Test my_view() as if it were deployed at /customer/details
response = my_view(request)
self.assertEqual(response.status_code, 200)