Question

I'm trying to write a test which tests if there is an object_list in the context passed by a Class Based View ListView.

Currently I have the code:

views.py

    class BlogView(ListView):
        model = Post
        template_name = 'core/blog.html'

model.py:

    class Post(models.Model):
        title = models.CharField(max_length=500)
        content = models.TextField()

tests.py

    class BlogListViewTests(TestCase):

        def setUp(self):
            self.resp = self.client.get('/blog/')

        def test_has_a_post_list(self):
            self.assertContains(u'post_list', self.resp.context)

When I run the test have the following error:

    Traceback (most recent call last):
      File "/home/breno/projects/blog/core/tests.py", line 36, in test_has_a_post_list
          self.assertContains(self.resp.context, 'post_list')
      File "/home/breno/.virtualenvs/blog/local/lib/python2.7/site-packages/django/test/testcases.py", line 325, in assertContains
          self.assertEqual(response.status_code, status_code,
     AttributeError: 'ContextList' object has no attribute 'status_code'

Printing the self.resp.context I saw a Tuble with the key u'post_list' inside:

[[{'False': False, 'None': None, 'True': True}, {u'paginator': None, u'post_list': [], u'object_list': [],...,]]

What's going wrong with my test? How can I make this test? Ty

Was it helpful?

Solution

class BlogListViewTests(TestCase):

    def setUp(self):
        self.resp = self.client.get('/blog/')

    def test_has_a_post_list(self):
        self.assertTrue('post_list' in self.resp.context)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top