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

navigator

.

id

.

watch

();

};

But that breaks the other test!

1 assertions of 2 passed, 1 failed.

1. initialize binds sign in button to navigator.id.request (1, 0, 1)

1. Died on test #1

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

missing argument 1 when calling function navigator.id.watch

2. initialize calls navigator.id.watch (0, 1, 1)

That was a puzzler—that “missing argument 1when calling functionnavigator.id.watch”

took me a while to figure out.

Turns out that

, in Firefox,

.watch

is a function on every

object. We’ll need to mock it out in the previous test too:

accounts/static/tests/tests.html.

test("initialize binds sign in button to navigator.id.request", function () {

var requestWasCalled = false;

var mockRequestFunction = function () { requestWasCalled = true; };

var mockNavigator = {

id: {

request: mockRequestFunction,

watch: function () {}

}

};

[...]

And we’re back to passing tests:

3 assertions of 3 passed, 0 failed.

1. initialize binds sign in button to navigator.id.request (0, 2, 2)

2. initialize calls navigator.id.watch (0, 1, 1)

Checking Call Arguments

We’re not calling the

watch

function correctly yet—it needs to know the current user,

and we have to set up a couple of callbacks for login and logout. Let’s start with the user:

accounts/static/tests/tests.html (ch15l042).

test("watch sees current user", function () {

var user = 'current user';

var token = 'csrf token';

var urls = {login: 'login url', logout: 'logout url'};

var mockNavigator = {

id: {

watch: sinon.mock()

}

};

Superlists.Accounts.initialize(mockNavigator, user, token, urls);

JavaScript Unit Tests Involving External Components: Our First Mocks!

|

267