HTML and CSS Reference
In-Depth Information
Listing 17.5 Expecting observers to be notified
"test dispatch should notify observers":
sinon.test(function (stub) {
var client = Object.create(ajax.cometClient);
var observers = client.observers;
stub(observers, "notify");
client.dispatch({ custom: [{ id:1234 }] });
assertCalledWith(observers.notify, "custom", { id:1234 });
})
17.1.3.2 Domain Specific Test Helpers
Another example of repeated patterns from Part III, Real-World Test-Driven
Development in JavaScript, that could be simplified by a higher-level ab-
straction is testing of event handlers. Given that the chat client uses the
custom dom.addEventHandler method in conjunction with Function.
prototype.bind to bind event handlers, we could extract the scaffolding needed
to test this into something like Listing 17.6.
Listing 17.6 Testing event handlers using a higher-level abstraction
"test should handle submit event with bound handleSubmit":
function () {
expectMethodBoundAsEventHandler(
this.controller, "handleSubmit", "submit", function () {
this.controller.setView(this.element);
}.bind(this)
);
}
This simple test replaces two original tests from the user form controller's test
case, and the imaginary helper method abstracts away some of the cruft related
to stubbing the handler method and addEventHandler , as well as obtaining a
reference to the handler function passed to it to verify that it is called with the object
as this .
When introducing domain and/or project specific test helpers such as this, we
can also test them to make sure they work as expected, and then use them throughout
the project, reducing the amount of scaffolding test code considerably.
 
Search WWH ::




Custom Search