HTML and CSS Reference
In-Depth Information
"test connect should start polling": function () {
this.client.url = "/my/url";
ajax.poll = stubFn({});
this.client.connect();
assert(ajax.poll.called);
assertEquals("/my/url", ajax.poll.args[0]);
}
});
Listing 16.11 shows the same listing using Sinon to handle stubs. Notice that
we still need the tearDown , but less manual juggling of the interfaces is required.
Listing 16.11 Using Sinon to handle stubs
TestCase("CometClientConnectTest", {
setUp: function () {
this.client = Object.create(ajax.cometClient);
},
tearDown: function () {
ajax.poll.restore();
},
"test connect should start polling": function () {
this.client.url = "/my/url";
sinon.stub(ajax, "poll").returns({});
this.client.connect();
assert(ajax.poll.calledWith("/my/url");
}
});
In addition to simplifying the stub management business, Sinon provides a more
fine-grained retrieval interface, resulting in tests that read better. But there is more;
Sinon provides a sandbox feature that automatically manages and restores stubs.
Listing 16.12 shows an example.
Listing 16.12 Automatically managing stubs with Sinon
"test connect should start polling":
sinon.test(function (stub) {
this.client.url = "/my/url";
 
Search WWH ::




Custom Search