HTML and CSS Reference
In-Depth Information
stub(ajax, "poll").returns({});
this.client.connect();
assert(ajax.poll.calledWith("/my/url"));
})
By wrapping the test function in a
sinon.test
call and using the
stub
method that is passed to it, stubs are strictly local and are automatically restored
upon the test's completion, even if the test throws exceptions. When using this
feature we can throw out all the stub related logic in both the
setUp
and
tearDown
methods.
When you have a lot of tests that need this kind of clean up you can also wrap
the entire test case object in a call to
sinon.testCase
, which is the same as
wrapping every test function in a call to
sinon.test
. Listing 16.13 shows an
example.
Listing 16.13
Automatically restoring stubs after each test
TestCase("CometClientConnectTest", sinon.testCase({
setUp: function (stub) {
/* ... */
stub(ajax, "poll").returns({});
},
"test connect should start polling": function () {
this.client.connect();
assert(ajax.poll.calledWith(this.client.url));
},
"test should not connect if connected": function () {
this.client.connect();
this.client.connect();
assert(ajax.poll.calledOnce);
},
/* ... */
});