Question

I'm sure I'm missing something hideously obvious here, but this test is currently failing:

def test_index_view_returns_correct_html_document(self):
        request = HttpRequest()
        response = LogIn(request)
        expected_html = render_to_string('login.html')
        self.assertEqual(response.content, expected_html)

It fails with the error ''HttpRequest' object has no attribute 'user''

The view that's being tested has this piece of code that checks whether a user is currently logged in, and throws a redirect if so:

if request.user.is_authenticated():
            return HttpResponseRedirect(reverse('index'))

What am I missing? I've looked in the docs but can't seem to find an explanation. I'm sure it's something obvious. Or I'm doing something wrong.

Was it helpful?

Solution

You've just constructed a bare request and passed it to the function, so it hasn't gone through any of the middleware - including the authentication one that adds the user object.

You should probably use the test client for this test, as it simulates the whole request/response cycle.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top