HTML and CSS Reference
In-Depth Information
assertCalledWith(ajax.poll, "/my/url");
sinon.assert.expose takes three arguments: the target object to inject
assertions into; whether or not assertions should be prefixed (i.e., true results in
“target.assertCalled”; false results in “target.called”); and finally whether or not
fail and failException should also be injected.
16.5.4 Stubbing and Node.js
Sinon exposes a CommonJS module, which means that it can also be used in a
CommonJS compliant runtime, such as Node.js. Listing 16.16 shows a test from
Chapter 14, Server-Side JavaScript with Node.js, in which we stub the getMes-
sagesSince test to return a promise object.
Listing 16.16 Stubbing in Node.js
var sinon = require("sinon");
/* ... */
testCase(exports, "chatRoom.waitForMessagesSince", {
/* ... */
"should yield existing messages":
sinon.test(function (test, stub) {
var promise = new Promise();
promise.resolve([{ id: 43 }]);
stub(, "getMessagesSince").returns(promise); (m) {
test.same([{ id: 43 }], m);
/* ... */
Note that Sinon takes care not to override the test object that Nodeunit passes
to the test. The stub function is passed after any arguments passed to the function
when it is called by the test runner.
Search WWH ::

Custom Search