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

1. Died on test #1

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

navigator.id.doSomethingElse is not a function

You see, the mock navigator object that we pass in is entirely under our control. It has

only the attributes and methods we give it. You can play around with it now if you like:

accounts/static/tests/tests.html.

var mockNavigator = {

id: {

request: mockRequestFunction,

doSomethingElse: function () { console.log("called me!");}

}

};

That will give you a pass, and if you open up the debug window, you’ll see:

[01:22:27.456] "called me!"

Does that help to see what’s going on? Let’s revert those last two changes, and tweak our

unit test so that it checks the

request

function is only called

after

we fire off the click

event. We also add some error messages to help see which of the two

equal

assertions

is failing:

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

var mockNavigator = {

id: {

request: mockRequestFunction

}

};

Superlists.Accounts.initialize(mockNavigator);

equal(requestWasCalled, false, 'check request not called before click');

$('#id_login').trigger('click');

equal(requestWasCalled, true, 'check request called after click');

Assertion messages (the third argument to

equal

), in QUnit, are ac‐

tually “success” messages. Rather than only being displayed if the test

fails, they are also displayed when the test passes. That’s why they have

the positive phrasing.

Now we get a neater failure:

1 assertions of 2 passed, 1 failed.

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

1. check request not called before click

Expected: false

Result: true

So let’s make it so that the call to

navigator.id.request

only happens if our

id_log

in

is clicked:

JavaScript Unit Tests Involving External Components: Our First Mocks!

|

263