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

you don’t know what the status of the later assertions is. As we’ll see in the next chapter,

if we ever break this view accidentally, we want to knowwhether it’s the saving of objects

that’s broken, or the type of response.

You may not always write perfect unit tests with single assertions on your first go, but

now feels like a good time to separate out our concerns:

lists/tests.py.

def

test_home_page_can_save_a_POST_request

(

self

):

request

=

HttpRequest

()

request

.

method

=

'POST'

request

.

POST

[

'item_text'

]

=

'A new list item'

response

=

home_page

(

request

)

self

.

assertEqual

(

Item

.

objects

.

count

(),

1

)

new_item

=

Item

.

objects

.

first

()

self

.

assertEqual

(

new_item

.

text

,

'A new list item'

)

def

test_home_page_redirects_after_POST

(

self

):

request

=

HttpRequest

()

request

.

method

=

'POST'

request

.

POST

[

'item_text'

]

=

'A new list item'

response

=

home_page

(

request

)

self

.

assertEqual

(

response

.

status_code

,

302

)

self

.

assertEqual

(

response

[

'location'

],

'/'

)

And we should now see six tests pass instead of five:

Ran 6 tests in 0.010s

OK

Rendering Items in the Template

Much better! Back to our to-do list:

Rendering Items in the Template

|

69