Assertionerror: b' Working with test classes in Django (TDD)

Asked

Viewed 83 times

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.

1 answer

1


Try to put self.assertEqual(response.content, 'Dados Corretos!.decode("utf-8")') or self.assertEqual(response.content.decode("utf-8"), 'Dados Corretos!').

This b in front means that the string is encoded in bytes and not in Unicode characters, so I suggested using the decode.

  • 1

    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!

  • 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

  • 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.