HTML and CSS Reference
In-Depth Information
Listing 12.24 Extracting fakeXMLHttpRequest
var fakeXMLHttpRequest = {
open: stubFn()
};
Because the fake object relies on stubFn , which is defined in lib/stub.js ,
we need to update jsTestDriver.conf to make sure the helper is loaded before
the fake object. Listing 12.25 shows the updated configuration file.
Listing 12.25 Updating jsTestDriver.conf to load files in correct order
server: http://localhost:4224
load:
- lib/stub.js
- lib/*.js
- src/*.js
- test/*.js
Next up, we update the test case by elevating the ajax.create stub to
setUp . To create the fakeXMLHttpRequest object we will use Object.
create from Chapter 7, Objects and Prototypal Inheritance, so place this func-
tion in lib/object.js . Listing 12.26 shows the updated test case.
Listing 12.26 Automate stubbing of ajax.create and XMLHttpRequest
TestCase("GetRequestTest", {
setUp: function () {
this.ajaxCreate = ajax.create;
this.xhr = Object.create(fakeXMLHttpRequest);
ajax.create = stubFn(this.xhr);
},
/* ... */
"test should obtain an XMLHttpRequest object":
function () {
ajax.get("/url");
assert(ajax.create.called);
},
"test should call open with method, url, async flag":
function () {
 
Search WWH ::




Custom Search