#20609: Documentation extension
-------------------------------+--------------------
     Reporter:  michel@…       |      Owner:  nobody
         Type:  Uncategorized  |     Status:  new
    Component:  Documentation  |    Version:  1.5
     Severity:  Normal         |   Keywords:
 Triage Stage:  Unreviewed     |  Has patch:  0
Easy pickings:  0              |      UI/UX:  0
-------------------------------+--------------------
 I had a lot of problems finding out why I cannot use request.user in my
 tests while using RequestFactory.

 this line in the documentation
 (https://docs.djangoproject.com/en/1.4/topics/testing/#the-request-
 factory) seems to say it:

 It does not support middleware. Session and authentication attributes must
 be supplied by the test itself if required for the view to function
 properly.

 But it would be nice to have an example with a user added, like:

 class TestCaseWithUser(TestCase):

     def setUp(self):
         # Every test needs access to the request factory.
         self.factory = RequestFactory()
         self.client = Client()
         self.user_foo = User.objects.create_user('foo', '[email protected]',
 'bar')

     def tearDown(self):
         # Delete those objects that are saved in setup
         self.user_foo.delete()

     def test_request_user(self):
         self.client.login( username='foo', password='bar')
         request = self.factory.post('/my/url/', {"somedata": "data"})
         # add a user so in the view to test request.user works
         request.user = self.user_foo
         nt.assert_equal(request.user,self.user_foo)

-- 
Ticket URL: <https://code.djangoproject.com/ticket/20609>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/058.a0946a795ce76ee2d95954c523575505%40djangoproject.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to