HTML and CSS Reference
In-Depth Information
Listing 16.22 Using the never method
"test should not connect if connected":
sinon.test(function (stub, mock) {
this.client.url = "/my/url";
stub(ajax, "poll").returns({});
this.client.connect();
mock(ajax).expects("poll").never();
this.client.connect();
})
The test looks different, but behaves exactly like the previous one; if the poll
method is called a second time, it will immediately throw an exception that fails the
test. The only difference between these two tests is the resulting exception message
in case they fail. Using once to expect only call will probably yield an error message
closer to the intended result than first stubbing the method and then mocking it
with the never modifier.
16.6.4 Expectations on the this Value
Mocks are capable of any kind of inspection possible with test spies. In fact, mocks
use test spies internally to record information about calls to them. Listing 16.23
shows one of the tests from the chat client's user form controller. It expects the
controller's handleSubmit method bound to it as the submit event handler.
Listing 16.23 Expecting the event handler to be bound to the controller
"test should handle event with bound handleSubmit":
sinon.test(function (stub, mock) {
var controller = this.controller;
stub(dom, "addEventHandler");
mock(controller).expects("handleSubmit").on(controller);
controller.setView(this.element);
dom.addEventHandler.getCall(0).args[2]();
})
This test shows how to use the test spy's retrieval interface to get the first call to
the dom.addEventHandler method, and then accessing its args array, which
contains the received arguments.
 
Search WWH ::




Custom Search