Using RequestFactory

Just as we used RequestFactory in the last chapter to test the index view, let's use it to write a test for the view that we need. Add the following new class to the bottom of solos/tests/test_views.py:

class SoloViewTestCase(TestCase):

    def setUp(self):
        self.factory = RequestFactory()

    def test_basic(self):
        """
        Test that the solo view returns a 200 response, uses
        the correct template, and has the correct context
        """
        request = self.factory.get('/solos/1/')

        response = SoloDetailView.as_view()(
            request,
            self.drum_solo.pk
        )

        self.assertEqual(response.status_code, 200)
          self.assertEqual(
              response.context_data['solo'].artist,
              'Rich'
          )
        with self\
            .assertTemplateUsed('solos/solo_detail.html'):
            response.render()

This should look familiar, ...

Get Test-Driven Development with Django now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.