HTML and CSS Reference
In-Depth Information
Listing 12.47 Completing the fake request
var fakeXMLHttpRequest = {
open: stubFn(),
send: stubFn(),
readyStateChange: function (readyState) {
this.readyState = readyState;
this.onreadystatechange();
}
};
Using this method, we can extract a helper method that accepts as arguments
a status code and a ready state, and returns an object with properties success
and failure , both indicating if the corresponding callback was called. This is a
bit of a leap because we haven't yet written any tests for the failure callback, but in
order to move along we will make a run for it. Listing 12.48 shows the new helper
function.
Listing 12.48 Request helper for tests
function forceStatusAndReadyState(xhr, status, rs) {
var success = stubFn();
var failure = stubFn();
ajax.get("/url", {
success: success,
failure: failure
});
xhr.status = status;
xhr.readyStateChange(rs);
return {
success: success.called,
failure: failure.called
};
}
Because this abstracts the whole body of a few tests, it was given a fairly verbose
name so as to not take away from the clarity of the tests. You'll be the judge of
whether the tests are now too abstract or still clear. Listing 12.49 shows the helper
in use.
 
Search WWH ::




Custom Search