HTML and CSS Reference
In-Depth Information
Listing 16.18 Verifying mocks automatically
"test connect should start polling":
sinon.test(function (stub, mock) {
var url = this.client.url = "/my/url";
The mock once again expects exactly one call—no more, no less. These three
lines replace the original four-line test along with both the setUp and tearDown
methods. Less code means less chance of bugs, less code to maintain, and less code
to read and understand. However, that alone does not necessarily mean you should
prefer mocks to stubs, or even use fakes at all.
16.6.2 Anonymous Mocks
Mocks, like stubs, can be simple anonymous functions to pass into the system.
All mocks, including anonymous ones, support the same interface as stubs to pre-
program them to return specific values or throw exceptions. Additionally, using
Sinon's sandbox, they can be automatically verified, allowing for really short and
concise tests.
Listing 16.19 revisits the observable test from Listing 16.6, this time using
mocks to create anonymous mock functions, one of which is set up to throw an
exception. As did the previous mocks, the anonymous mocks expect exactly one
Listing 16.19 Using mocks to verify observable's notify
"test observers should be notified even when some fail":
sinon.test(function(stub, mock) {
var observable = Object.create(tddjs.util.observable);
Because sinon.test keeps record of all stubs and mocks, and automatically
verifies mocks, this test does not need local references to the two mock functions.
Search WWH ::

Custom Search