0

This is in tests.py

class QuizDetailView(TestCase):

def test_quiz_detail_has_html(self):
    x = 1 # initial quiz
    path = reverse('quiz:detail', args=(x,))
    print('\n** quiz:detail = ', path)
    print('** HttpResponse = ', HttpResponse(path))
    print('-- self.client.get = ', self.client.get(path))
    print('-- self.client.get /quiz/1 = ', self.client.get('/quiz/1'))
    print('-- self.client.get /quiz/1/ = ', self.client.get('/quiz/1/'))

This is the output

$ python manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.
** quiz:detail =  /quiz/1/
** HttpResponse =  <HttpResponse status_code=200, "text/html; charset=utf-8">
-- self.client.get =  <HttpResponseNotFound status_code=404, "text/html">
-- self.client.get /quiz/1 =  <HttpResponsePermanentRedirect status_code=301, "text/html; charset=utf-8", url="/quiz/1/">
-- self.client.get /quiz/1/ =  <HttpResponseNotFound status_code=404, "text/html">

So, HttpResponse is working but why does self.client not? If I manually go to the go to the webpage, then it works fine.

  • Those are two completely different things. `HttpResponse` just returns a response object with content that is passed to it. It doesn't simulate a request. As for why `self.client.get` returns a 404, this is presumably because you don't have a quiz matching ID 1 in your test database - your test doesn't create one. – solarissmoke Dec 24 '19 at 04:45
  • @solarissmoke I added 'Quiz.objects.create()' and that cleared things up. I had data in my actual database, but tests.py creates a new empty database, I had not appreciated that. I needed to put data in that new empty test database. Thanks – FlatEarthApproximation Dec 24 '19 at 14:55

0 Answers0