HTML and CSS Reference
In-Depth Information
this.xhr.onreadystatechange();
assert(success.called);
}
By pre-programming tddjs.isLocal to always return true , we force the
request interface through the path that handles local requests. Gerard Meszaros
calls these kinds of stubs Responders , and they are commonly used to test the happy
path through a system.
16.3.3 Stubbing to Cause Trouble
Similar to the responder, a Saboteur is a stub that behaves strangely by returning
unexpected values or even throwing exceptions. Injecting such a stub into the
system allows us to test how well it deals with uncooperative objects and unexpected
behavior.
Listing 16.6 shows a test from Chapter 11, The Observer Pattern, in which a
saboteur is used to verify that all observers are notified even when some of them
throw exceptions.
Listing 16.6 Using a saboteur to ensure all observers are notified
"test should notify all even when some fail": function () {
var observable = new tddjs.util.Observable();
var observer1 = function () { throw new Error("Oops"); };
var observer2 = function () { observer2.called = true; };
observable.addObserver(observer1);
observable.addObserver(observer2);
observable.notifyObservers();
assertTrue(observer2.called);
}
The saboteur is a useful tool when bulletproofing interfaces intended for a
wide audience. They can also be used to mimic a lot of strange behavior in certain
browsers, helping us write code that survives even the fiercest host objects.
16.4 Test Spies
Test spies are objects and functions that record information about their usage
throughout the system under test. They are useful when determining a function's
success is not easily accomplished by inspecting its return value or changes to the
 
 
Search WWH ::




Custom Search