HTML and CSS Reference
In-Depth Information
the test succeeds. To fix this we can move the reference and restoring of the original
method to the setUp and tearDown methods respectively. Listing 12.16 shows
the updated test case.
Listing 12.16 Stubbing and restoring ajax.create safely
TestCase("GetRequestTest", {
setUp: function () {
this.ajaxCreate = ajax.create;
},
tearDown: function () {
ajax.create = this.ajaxCreate;
},
/* ... */
"test should obtain an XMLHttpRequest object":
function () {
ajax.create = function () {
ajax.create.called = true;
};
ajax.get("/url");
assert(ajax.create.called);
}
});
Before we fix the next problem, we need to implement the method in question.
All we have to do is add a single line inside ajax.get , as in Listing 12.17.
Listing 12.17 Creating the object
function get(url) {
/* ... */
var transport = tddjs.ajax.create();
}
With this single line in place the tests go green again.
12.4.2.2 Automating Stubbing
The next issue with the stubbing solution is that it's fairly verbose. We can mitigate
this by extracting a helper method that creates a function that sets a flag when called,
 
Search WWH ::




Custom Search