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

var watchCallArgs = mockNavigator.id.watch.firstCall.args[0];

equal(watchCallArgs.loggedInUser, user, 'check user');

});

We have a very similar setup (which is a code smell incidentally—on the next test, we’re

going to want to do some de-duplication of test code). Then we use the

.first

Call.args[0]

property on the mock to check on the parameter we passed to the

watch

function (

args

being a list of positional arguments). That gives us:

3. watch sees current user (1, 0, 1)

1. Died on test #1

@file:///workspace/superlists/accounts/static/tests/tests.html:72:

watchCallArgs is undefined

because we’re not currently passing any arguments to

watch

. Step by step, we can do:

accounts/static/accounts.js (ch15l043).

navigator

.

id

.

watch

({});

and get a clearer error message:

3. watch sees current user (1, 0, 1)

1. check user

Expected: "current user"

Result: undefined

and fix it thusly:

accounts/static/accounts.js (ch15l044).

var

initialize

=

function

(

navigator

,

user

,

token

,

urls

) {

[...]

navigator

.

id

.

watch

({

loggedInUser

:

user

});

Good.

4 assertions of 4 passed, 0 failed.

QUnit setup and teardown, Testing Ajax

Next we need to check the

onlogin

callback, which is called when Persona has some

user authentication information, and we need to send it up to our server for validation.

That involves an Ajax call (

$.post

), and they’re normally quite hard to test, but sinon.js

has a helper called

fake XMLHttpRequest .

This patches out the native JavaScript

XMLHttpRequest

class, so it’s good practice to

make sure we restore it afterwards. This gives us a good excuse to learn about QUnit’s

setup

and

teardown

methods—they are used in a function called

module

, which acts a

bit like a

unittest.TestCase

class, and groups all the tests that follow it together.

268

|

Chapter 15: User Authentication, Integrating Third-Party Plugins, and Mocking with JavaScript