HTML and CSS Reference
In-Depth Information
writing this test, I made a typo, accidentally writing var expected =
this.xhr.headers . It's an easy mistake to make. Running the test right away
made me aware that something was amiss as the test was passing. Inspecting it
one more time alerted me to the typo. Not running the test before writing the
implementation would have made it impossible to discover the error. No matter
how we had eventually implemented the headers, as long as it didn't result in an
exception or a syntax error, the test would have passed, lulling us into the false
illusion that everything is fine. Always run tests after updating either the tests or the
implementation!
The implementation in Listing 13.23 is fairly mundane.
Listing 13.23 Passing on the headers
function start() {
/* ... */
ajax.request(this.url, {
complete: function () {
setTimeout(function () {
poller.start();
}, interval);
},
headers: poller.headers
});
}
Next up, we want to ensure all the callbacks are passed along as well. We'll
start with the success callback. To test that it is passed we can use the complete
method we added to the fake XMLHttpRequest object previously. This simulates
a successful request, and thus should call the success callback. Listing 13.24 shows
the test.
Listing 13.24 Expecting the success callback to be called
"test should pass success callback": function () {
this.poller.success = stubFn();
this.poller.start();
this.xhr.complete();
assert(this.poller.success.called);
}
 
Search WWH ::




Custom Search