Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i access the response context of a view tested without the test client?

I have a function which i call from a unittest. From setting some debug traces i know the function worked like a charm and has all the values correctly prepared for return.

This is what my testcode looks like (see where my ipdb.set_trace() is ):

@override_settings(REGISTRATION_OPEN=True)
def test_confirm_account(self):
    """ view that let's a user confirm account creation and username
        when loggin in with social_auth """    
    request = self.factory.get('')
    request.user = AnonymousUser()
    request.session={}
    request.session.update({self.pipename:{'backend':'facebook',
                                           'kwargs':{'username':'Chuck Norris','response':{'id':1}}}})

    # this is the function of which i need the context:
    response = confirm_account(request)
    self.assertEqual(response.context['keytotest'],'valuetotest')

From what i know from this part of the Django docs, i would be able to access response.context when i have used the testing client. But when i try to access response.context like i did it, i get this:

AttributeError: 'HttpResponse' object has no attribute 'context'

Is there a way to get the special HttpResponse object of the client, without using the client?

like image 258
marue Avatar asked Apr 13 '12 20:04

marue


People also ask

What is self client in Django?

self. client , is the built-in Django test client. This isn't a real browser, and doesn't even make real requests. It just constructs a Django HttpRequest object and passes it through the request/response process - middleware, URL resolver, view, template - and returns whatever Django produces.

What is RequestFactory in Django?

The request factory The RequestFactory shares the same API as the test client. However, instead of behaving like a browser, the RequestFactory provides a way to generate a request instance that can be used as the first argument to any view.

What is test case in Django?

The best base class for most tests is django. test. TestCase. This test class creates a clean database before its tests are run, and runs every test function in its own transaction. The class also owns a test Client that you can use to simulate a user interacting with the code at the view level.


2 Answers

The RequestFactory does not touch the Django middleware, and as such, you will not generate a context (i.e. no ContextManager middleware).

If you want to test the context, you should use the test client. You can still manipulate the construction of the request in the test client either using mock or simply saving your session ahead of time in the test, such as:

from django.test import Client
c = Client()
session = c.session
session['backend'] = 'facebook'
session['kwargs'] = {'username':'Chuck Norris','response':{'id':1}}
session.save()

Now when you load the view with the test client, you'll be using the session as you set it, and when you use response = c.get('/yourURL/'), you can then reference the response context using response.context as desired.

like image 85
jlovison Avatar answered Oct 05 '22 11:10

jlovison


The "response.context" is incorrect for new django versions but you can use response.context_data to get the same context that passed to TemplateResponse.

like image 25
F.Tamy Avatar answered Oct 05 '22 11:10

F.Tamy