HTML and CSS Reference
In-Depth Information
},
"test should prevent event default action": function () {
this.controller.handleSubmit(this.event);
assert(this.event.preventDefault.called);
}
Assuming we fell completely in love with mocks, we might have gone and
mocked that model object rather than stubbing it. Doing so means that any test may
fail as a result of unexpected interaction with the model object—even the tests that
focus on something entirely different, such as the event object's preventDefault
method being called. Mocks should be treated with the same respect as assertions;
don't add ones that test things you already know, and don't add ones that don't
support the goal of the test.
In the case of using a top-down approach to implement, e.g., the user interface
before dependencies such as model objects, both mocks and stubs are good choices.
In this case tests will have to rely on behavior verification alone in any case, meaning
that stubs lose their advantage of supporting less implementation-specific state ver-
ification. In the general sense, however, mocks always rely on behavior verification;
thus, they are inherently more implementation specific.
16.8 Summary
In this chapter we have taken a deep dive into the concept of test doubles , focusing
mainly on stubs , spies and, mocks . Although we have used stubs and spies frequently
throughout Part III, Real-World Test-Driven Development in JavaScript, looking at
them from a wider angle has allowed us to coin some common usage patterns and
describe them using established terminology.
Having gotten through all of five sample projects without one, we investigated
the effects of using a stubbing and mocking library in tests. The manual approach
is easy to employ in JavaScript, and will take you far. Still, using a dedicated library
can reduce the stubbing and mocking related scaffolding, which leads to leaner tests
and less repetition. Removing manual stubbing logic in favor of a well tested library
also reduces chances of bugs in tests.
In light of Sinon, the stubbing and mocking library, mocks were finally pre-
sented. Mocks are stubs pre-programmed with expectations that translate into be-
havior verification. Mocks fail early, by throwing an exception immediately upon
receiving an unexpected call.
 
 
Search WWH ::




Custom Search