HTML and CSS Reference
In-Depth Information
state of objects with which it interacts. You may recognize that the stubs in Part III,
Real-World Test-Driven Development in JavaScript, have frequently been used this
way; in fact, test spies are usually implemented as recording stubs.
16.4.1 Testing Indirect Inputs
The request interface we built in Chapter 12, Abstracting Browser Differences:
Ajax, provides many examples of using test spies to verify a test. The interface
was built to provide a higher level abstraction over the XMLHttpRequest object,
and as such its success is mainly defined by its ability to correctly map calls to
the underlying object. Listing 16.7 shows a test that verifies that requesting a URL
causes the XMLHttpRequest object's send method to be called.
Listing 16.7 Using a test spy to verify that a method is called on an indirect input
TestCase("GetRequestTest", {
setUp: function () {
this.ajaxCreate = ajax.create;
this.xhr = Object.create(fakeXMLHttpRequest);
ajax.create = stubFn(this.xhr);
},
/* ... */
"test should call send": function () {
ajax.get("/url");
assert(this.xhr.send.called);
}
});
The setUp pre-programs ajax.create to return a fakeXMLHttp
Request instance, which is assigned to the test for behavior verification. The
object returned from ajax.create is an indirect input to the ajax.request
method. Stubs or mocks are usually the only way to test the effects of an indirect
input on the system under test.
16.4.2 Inspecting Details about a Call
A test spy need not restrict itself to recording whether or not a function was called.
It can record any kind of data about its use. The stubFn helper used throughout
most of Part III, Real-World Test-Driven Development in JavaScript, also recorded
 
Search WWH ::




Custom Search