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

[...]

FAIL: test_create_returns_new_list_object

self.fail()

AssertionError: None

FAILED (failures=1, errors=3)

We flesh it out:

lists/tests/test_models.py (ch19l039-2).

def

test_create_returns_new_list_object

(

self

):

returned

=

List

.

create_new

(

first_item_text

=

'new item text'

)

new_list

=

List

.

objects

.

first

()

self

.

assertEqual

(

returned

,

new_list

)

AssertionError: None != <List: List object>

And we add our return value:

lists/models.py (ch19l039-3).

@staticmethod

def

create_new

(

first_item_text

,

owner

=

None

):

list_

=

List

.

objects

.

create

(

owner

=

owner

)

Item

.

objects

.

create

(

text

=

first_item_text

,

list

=

list_

)

return

list_

And that gets us to a fully passing test suite:

$

python3 manage.py test lists

[...]

Ran 50 tests in 0.169s

OK

One More Test

That’s our code for saving list owners test-driven all the way down and working. But

our functional test isn’t passing quite yet:

$

python3 manage.py test functional_tests.test_my_lists

selenium.common.exceptions.NoSuchElementException: Message: 'Unable to locate

element: {"method":"link text","selector":"Reticulate splines"}' ; Stacktrace:

It’s because we have one last feature to implement, the

.name

attribute on list objects.

Again, we can grab the test and code from the last chapter:

lists/tests/test_models.py (ch19l040).

def

test_list_name_is_first_item_text

(

self

):

list_

=

List

.

objects

.

create

()

Item

.

objects

.

create

(

list

=

list_

,

text

=

'first item'

)

Item

.

objects

.

create

(

list

=

list_

,

text

=

'second item'

)

self

.

assertEqual

(

list_

.

name

,

'first item'

)

358

|

Chapter 19: Test Isolation, and “Listening to Your Tests”