HTML and CSS Reference
In-Depth Information
and allows access to this flag. Listing 12.18 shows one such possible method. Save
it in lib/stub.js .
Listing 12.18 Extracting a function stubbing helper
function stubFn() {
var fn = function () {
fn.called = true;
};
fn.called = false;
return fn;
}
Listing 12.19 shows the updated test.
Listing 12.19 Using the stub helper
"test should obtain an XMLHttpRequest object": function () {
ajax.create = stubFn();
ajax.get("/url");
assert(ajax.create.called);
}
Now that we know that ajax.get obtains an XMLHttpRequest object we
need to make sure it uses it correctly. The first thing it should do is call its open
method. This means that the stub helper needs to be able to return an object.
Listing 12.20 shows the updated helper and the new test expecting open to be
called with the right arguments.
Listing 12.20 Test that the open method is used correctly
function stubFn(returnValue) {
var fn = function () {
fn.called = true;
return returnValue;
};
fn.called = false;
return fn;
}
 
Search WWH ::




Custom Search