return
redirect
(
list_
)
else
:
return
render
(
request
,
'home.html'
, {
"form"
:
form
})
def
new_list2
(
request
):
[
...
]
One of the benefits of integrated tests is that they help you to catch
less predictable interactions like this. We’d forgotten about to write a
test for the case where the user is not authenticated, but because the
integrated tests use the stack all the way down, errors from the mod‐
el layer came up to let us know we’d forgotten something:
$
python3 manage.py test lists
[...]
Ran 48 tests in 0.175s
OK
The Moment of Truth (and the Risks of Mocking)
So let’s try switching out our old view, and activating our new view. We can make the
swap in
urls.py
:
lists/urls.py.
[
...
]
url
(
r'^new$'
,
'lists.views.new_list2'
,
name
=
'new_list'
),
We should also remove the
unittest.skip
from our integrated test class, and make it
point at our new view (
new_list2
), to see if our new code for list owners really works:
lists/tests/test_views.py (ch19l033).
class
NewListViewIntegratedTest
(
TestCase
):
def
test_saving_a_POST_request
(
self
):
[
...
]
def
test_list_owner_is_saved_if_user_is_authenticated
(
self
):
request
=
HttpRequest
()
request
.
user
=
User
.
objects
.
create
(
=
'a@b.com'
)
request
.
POST
[
'text'
]
=
'new list item'
new_list2
(
request
)
list_
=
List
.
objects
.
first
()
self
.
assertEqual
(
list_
.
owner
,
request
.
user
)
So what happens when we run our tests? Oh no!
ERROR: test_list_owner_is_saved_if_user_is_authenticated
[...]
ERROR: test_saving_a_POST_request
[...]
ERROR: test_redirects_after_POST
354
|
Chapter 19: Test Isolation, and “Listening to Your Tests”