#35494: Testing Class-based Views Clarification
------------------------------------------------+--------------------------
               Reporter:  Matthew Pava          |          Owner:  nobody
                   Type:  Cleanup/optimization  |         Status:  new
              Component:  Documentation         |        Version:  5.1
               Severity:  Normal                |       Keywords:  test cbv
           Triage Stage:  Unreviewed            |      Has patch:  0
    Needs documentation:  0                     |    Needs tests:  0
Patch needs improvement:  0                     |  Easy pickings:  1
                  UI/UX:  0                     |
------------------------------------------------+--------------------------
 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)
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35494>
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 django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018fe464943e-56615793-1bca-4f6a-b727-83a1aac3268b-000000%40eu-central-1.amazonses.com.

Reply via email to