django unit test class based view error No JSON object could be decoded
Question: I want to test my class based view.

Here is the models.py file:

    
    
    class TodoList(models.Model):
    
        todoitem = models.CharField(max_length=200)
        description = models.TextField()
        pub_date = models.DateField(auto_now_add=True)
    

Here is my views.py file

    
    
    class ListTodoView(ListView):
    
        model = TodoList
        template_name = 'todolist_listview.html'
    

Here is my unit test:

    
    
    class TestToDoListView(TestCase):
    
        def test_list_view(self):
            url = reverse('notes-list')
            resp = self.client.get(url)
            self.assertEquals(resp.status_code, 200)
            data = json.loads(resp.content) # getting error here No JSON object could be decoded
            self.assertEquals(len(data), 1)
    

What is the mistake that I am doing? I have made sure, I have placed all the
imports at place.

Here is the stacktrace:

    
    
    ======================================================================
    
    ERROR: test_list_view 
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/home/.../tests/test_views.py", line 21, in test_list_view
        data = json.loads(resp.content)
      File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
        return _default_decoder.decode(s)
      File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode
        raise ValueError("No JSON object could be decoded")
    ValueError: No JSON object could be decoded
    


Answer: Why do you test like this? What is the code in your html template? If there is
a html code, it wrong to test like json data.

If you need return json data, it is wrong way to do.

