﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35494	Testing Class-based Views Clarification	Matthew Pava	nobody	"We have this code for testing class-based views, and I think we need some clarification in the documentation.
https://docs.djangoproject.com/en/dev/topics/testing/advanced/#testing-class-based-views

{{{
from django.test import RequestFactory, TestCase
from .views import HomeView


class HomePageTest(TestCase):
    def test_environment_set_in_context(self):
        request = RequestFactory().get(""/"")
        view = HomeView()
        view.setup(request)

        context = view.get_context_data()
        self.assertIn(""environment"", context)
}}}

The `RequestFactory().get()` method returns a response, and not a request.
On top of that `TestCase` already has access to a `RequestFactory` object called `client`.
Lastly, the response object is not a request object that you would pass into the `view.setup` function. You can access the request object from the client. This is what I have:

{{{
from django.test import TestCase
from .views import HomeView


class HomePageTest(TestCase):
    def test_environment_set_in_context(self):
        self.client.get(""/"")
        view = HomeView()
        view.setup(self.client.request)

        context = view.get_context_data()
        self.assertIn(""environment"", context)
}}}

"	Cleanup/optimization	closed	Documentation	5.1	Normal	invalid	test cbv	Mariusz Felisiak	Unreviewed	0	0	0	0	1	0
