HTML and CSS Reference
In-Depth Information
Listing 13.5
Expecting
start
to throw an exception on missing URL
"test start should throw exception for missing URL":
function () {
var poller = Object.create(ajax.poller);
assertException(function () {
poller.start();
}, "TypeError");
}
As usual, we run the test before implementing it. The first run coughs up an error
stating that there is no
Object.create
method. To fix this we fetch it from Chap-
ter 7,
Objects and Prototypal Inheritance,
and stick it in
tdd.js
. What happens next
is interesting; the test passes. Somehow a
TypeError
is thrown, yet we haven't done
anything other than defining the object. To see what's happening, we edit the test and
remove the
assertException
call, simply calling
poller.start()
directly
in the test. JsTestDriver should pick up the exception and tell us what's going on.
As you might have guessed, the missing
start
method triggers a
TypeError
of its own. This indicates that the test isn't good enough. To improve the situation we
add another test stating that there should be a
start
method, as seen in Listing 13.6.
Listing 13.6
Expecting the poller to define a
start
method
"test should define a start method":
function () {
assertFunction(ajax.poller.start);
}
With this test in place, we now get a failure stating that
start
was expected to
be a function, but rather was
undefined
. The previous test still passes. We will
fix the newly added test by simply adding a
start
method, as in Listing 13.7.
Listing 13.7
Adding the
start
method
(function () {
var ajax = tddjs.namespace("ajax");
function start() {
}
ajax.poller = {
start: start
};
}());