Background Image
Table of Contents Table of Contents
Previous Page  228 / 478 Next Page
Information
Show Menu
Previous Page 228 / 478 Next Page
Page Background

self.assertEqual(response.content.decode(), expected_html)

AssertionError: '<!DO[596 chars] <input class="form-control input-lg"

id="[342 chars]l>\n' != '<!DO[596 chars] \n \n

[233 chars]l>\n'

That error message is impossible to read though. Let’s clarify its message a little:

lists/tests/test_views.py (ch11l016).

class

HomePageTest

(

TestCase

):

maxDiff

=

None

#

[

...

]

def

test_home_page_returns_correct_html

(

self

):

request

=

HttpRequest

()

response

=

home_page

(

request

)

expected_html

=

render_to_string

(

'home.html'

)

self

.

assertMultiLineEqual

(

response

.

content

.

decode

(),

expected_html

)

#

assertMultiLineEqual

is useful for comparing long strings; it gives you a diff-

style output, but it truncates long diffs by default…

…so that’s why we also need to set

maxDiff = None

on the test class.

Sure enough, it’s because our

render_to_string

call doesn’t know about the form:

[...]

<form method="POST" action="/lists/new">

- <input class="form-control input-lg" id="id_text"

name="text" placeholder="Enter a to-do item" type="text" />

+

[...]

But we can fix that:

lists/tests/test_views.py.

def

test_home_page_returns_correct_html

(

self

):

request

=

HttpRequest

()

response

=

home_page

(

request

)

expected_html

=

render_to_string

(

'home.html'

, {

'form'

:

ItemForm

()})

self

.

assertMultiLineEqual

(

response

.

content

.

decode

(),

expected_html

)

And that gets us back to passing. We’ve now reassured ourselves enough that the be‐

haviour has stayed the same, so it’s now OK to delete the two old tests. The

assertTem

plateUsed

and

response.context

checks from the new test are sufficient for testing a

basic view with a GET request.

That leaves us with just two tests in

HomePageTest

:

lists/tests/test_views.py (ch11l017).

class

HomePageTest

(

TestCase

):

def

test_home_page_renders_home_template

(

self

):

[

...

]

def

test_home_page_uses_item_form

(

self

):

[

...

]

200

|

Chapter 11: A Simple Form