1
I’m starting my test studies with Django and I’m getting an error where apparently it shouldn’t, so much so that the description of traceback leaves me quite confused.
I have a class that tests whether the return of my view is compatible with its preset value, something simple that looks like this:
from django.test import TestCase, Client
from django.core.urlresolvers import reverse
...
class TestMinhaView(TestCase):
def setUp(self):
self.url = reverse('core:spy')
self.client = Client()
def test_get(self):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, 'Dados Corretos!')
Made to test the return of this View:
from django.views import generic
from django.http.response import HttpResponse
...
class MinhaView(generic.View):
def get(self, request, *args, **kwargs):
return HttpResponse('Dados Corretos!')
After modeling the classes, it’s time to test! Type in the terminal:
manage.py test
and then he returns to me the curious error in his Traceback:
self.assertEqual(response.content, 'Dados Corretos!')
AssertionError: b'Dados Corretos!' != 'Dados Corretos!'
This b makes me curious. What could it be? Because I can’t find a logical error since the information in the test and functional class is exactly the same.
I thank in advance the patience of those who are willing, and forgiveness if it is of a beginner level. Haha.
Hello! Thanks for the explanation and response, his second alternative worked! (the first he considered the .Decode instruction as part of the literal string not running it). I never thought the answer would come out so fast in less than 10 minutes! Haha, thank you!
– Ronald Rodrigues
actually the first one was a "typo" of mine, it was supposed to have written 'Correct Data! '. Decode("utf-8"). But if it worked the other way it doesn’t even matter
– Gabriel Belini
This also does not work, because what I realized it is nested and requires that the string has passed through the Meeting before using Code. 'Correct Data! '. Decode("utf-8") returns an error telling me that the object has no such method.
– Ronald Rodrigues