Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

…stFactory

Thanks michel@ for the suggestion.

Backport of 067e042 from master.
  • Loading branch information...
commit 75041d5ea3821912abf8b9d1cd5477228a78cb23 1 parent 2f9e548
@onceuponatimeforever onceuponatimeforever authored timgraham committed
Showing with 9 additions and 2 deletions.
  1. +9 −2 docs/topics/testing/advanced.txt
View
11 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)
Please sign in to comment.
Something went wrong with that request. Please try again.