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

one thing at a time. While our application only supports one list, this is the only URL

that makes sense. We’re still moving forwards, in that we’ll have a different URL for our

list and our home page, which is a step along the way to a more REST-ful design. Later,

when we have multiple lists, it will be easy to change.

Another way of thinking about it is as a problem-solving technique:

our new URL design is currently not implemented, so it works for 0

items. Ultimately, we want to solve for

n

items, but solving for 1 item

is a good step along the way.

Running the unit tests gives us an expected fail:

$

python3 manage.py test lists

[...]

AssertionError: '/' != '/lists/the-only-list-in-the-world/'

We can go adjust our

home_page

view in

lists/views.py

:

lists/views.py.

def

home_page

(

request

):

if

request

.

method

==

'POST'

:

Item

.

objects

.

create

(

text

=

request

.

POST

[

'item_text'

])

return

redirect

(

'/lists/the-only-list-in-the-world/'

)

items

=

Item

.

objects

.

all

()

return

render

(

request

,

'home.html'

, {

'items'

:

items

})

Of course, that will now totally break the functional tests, because there is no such URL

on our site yet. Sure enough, if you run them, you’ll find they fail just after trying to

submit the first item, saying that they can’t find the list table; it’s because URL

/the-only-

list-in-the-world/

doesn’t exist yet!

self.check_for_row_in_list_table('1: Buy peacock feathers')

[...]

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

element: {"method":"id","selector":"id_list_table"}' ; Stacktrace:

So, let’s build a special URL for our one and only list.

Testing Views, Templates, and URLs Together with the

Django Test Client

In previous chapters we’ve used unit tests that check the URL resolution explicitly, that

test view functions by actually calling them, and that check that views render templates

correctly too. Django actually provides us with a little tool that can do all three at once,

which we’ll use now.

Testing Views, Templates, and URLs Together with the Django Test Client

|

87