Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.5.x] Fixed #20609 -- Documented how to use request.user with Reque…

…stFactory

Thanks michel@ for the suggestion.

Backport of 75041d5 from stable/1.5.x.
  • Loading branch information...
commit 583f340d7dd52c6182530fd4ca798d01c4d93609 1 parent 44df41e
Susan  Tan authored July 02, 2013 timgraham committed July 03, 2013

Showing 1 changed file with 9 additions and 2 deletions. Show diff stats Hide diff stats

  1. 11  docs/topics/testing/advanced.txt
11  docs/topics/testing/advanced.txt
@@ -37,18 +37,25 @@ Example
37 37
 
38 38
 The following is a simple unit test using the request factory::
39 39
 
40  
-    from django.utils import unittest
  40
+    from django.contrib.auth.models import User
  41
+    from django.test import TestCase
41 42
     from django.test.client import RequestFactory
42 43
 
43  
-    class SimpleTest(unittest.TestCase):
  44
+    class SimpleTest(TestCase):
44 45
         def setUp(self):
45 46
             # Every test needs access to the request factory.
46 47
             self.factory = RequestFactory()
  48
+            self.user = User.objects.create_user(
  49
+                first_name='jacob', email='jacob@…', password='top_secret')
47 50
 
48 51
         def test_details(self):
49 52
             # Create an instance of a GET request.
50 53
             request = self.factory.get('/customer/details')
51 54
 
  55
+            # Recall that middleware are not suported. You can simulate a
  56
+            # logged-in user by setting request.user manually.
  57
+            request.user = self.user
  58
+
52 59
             # Test my_view() as if it were deployed at /customer/details
53 60
             response = my_view(request)
54 61
             self.assertEqual(response.status_code, 200)

0 notes on commit 583f340

Please sign in to comment.
Something went wrong with that request. Please try again.