![Show Menu](styles/mobile-menu.png)
![Page Background](./../common/page-substrates/page0359.png)
else
:
return
render
(
request
,
'home.html'
, {
"form"
:
form
})
But it won’t actually work, because we don’t know how to save a list owner yet:
self.assertEqual(list_.owner, request.user)
AttributeError: 'List' object has no attribute 'owner'
A Decision Point: Whether to Proceed to the Next Layer with a Failing
Test
In order to get this test passing, as it’s written now, we have to move down to the model
layer. However, it means doing more work with a failing test, which is not ideal.
The alternative is to rewrite the test to make it more
isolated
from the level below, using
mocks.
On the one hand, it’s a lot more effort to use mocks, and it can lead to tests that are
harder to read. On the other hand, imagine if our app was more complex, and there
were several more layers between the outside and the inside. Imagine leaving three or
four or five layers of tests, all failingwhile we wait to get to the bottom layer to implement
our critical feature. While tests are failing, we’re not sure that layer really works, on its
own terms, or not. We have to wait until we get to the bottom layer.
This is a decision point you’re likely to run into in your own projects. Let’s investigate
both approaches. We’ll start by taking the shortcut, and leaving the test failing. In the
next chapter, we’ll come back to this exact point, and investigate how things would have
gone if we’d used more isolation.
Let’s do a commit, and then
tag
the commit as a way of remembering our position for
the next chapter:
$
git commit -am"new_list view tries to assign owner but cant"
$
git tag revisit_this_point_with_isolated_tests
Moving Down to the Model Layer
Our outside-in design has driven out two requirements for the model layer: we want to
be able to assign an owner to a list using the attribute
.owner
, and we want to be able
to access the list’s owner with the API
owner.list_set.all
.
Let’s write a test for that:
lists/tests/test_models.py (ch18l018).
from
django.contrib.auth
import
get_user_model
User
=
get_user_model
()
[
...
]
class
ListModelTest
(
TestCase
):
Moving Down to the Model Layer
|
331