HttpResponseRedirect' object has no attribute 'client'
Question: Django 1.9.6

I'd like to write some unit test for checking redirection.

Could you help me understand what am I doing wrongly here.

Thank you in advance.

The test:

    
    
    from django.test import TestCase
    from django.core.urlresolvers import reverse
    from django.http.request import HttpRequest
    from django.contrib.auth.models import User
    
    class GeneralTest(TestCase):
    
        def test_anonymous_user_redirected_to_login_page(self):
            user = User(username='anonymous', email='vvv@mail.ru', password='ttrrttrr')
            user.is_active = False        
            request = HttpRequest()
            request.user = user
            hpv = HomePageView()
            response = hpv.get(request)
            self.assertRedirects(response, reverse("auth_login"))
    

The result:

## ERROR: test_anonymous_user_redirected_to_login_page
(general.tests.GeneralTest)

Traceback (most recent call last): File
"/home/michael/workspace/photoarchive/photoarchive/general/tests.py", line 44,
in test_anonymous_user_redirected_to_login_page self.assertRedirects(response,
reverse("auth_login")) File
"/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-
packages/django/test/testcases.py", line 326, in assertRedirects
redirect_response = response.client.get(path, QueryDict(query),
AttributeError: 'HttpResponseRedirect' object has no attribute 'client'

* * *

Ran 3 tests in 0.953s

What pdb says:

    
    
    -> self.assertRedirects(response, reverse("auth_login"))
    (Pdb) response
    <HttpResponseRedirect status_code=302, "text/html; charset=utf-8", url="/accounts/login/">
    


Answer: You need to add a client to the response object. See the updated code below.

    
    
    from django.test import TestCase, Client
    from django.core.urlresolvers import reverse
    from django.http.request import HttpRequest
    from django.contrib.auth.models import User
    
    class GeneralTest(TestCase):
    
        def test_anonymous_user_redirected_to_login_page(self):
            user = User(username='anonymous', email='vvv@mail.ru', password='ttrrttrr')
            user.is_active = False        
            request = HttpRequest()
            request.user = user
            hpv = HomePageView()
            response = hpv.get(request)
            response.client = Client()
            self.assertRedirects(response, reverse("auth_login"))
    

