HTML and CSS Reference
In-Depth Information
Before we get into the nitty-gritty of stubs, mocks, and the difference between
them, we will explore our options at the verification stage. As we will see shortly, veri-
fication strategy is a central issue when making the choice between stubs and mocks.
16.2.1 State Verification
Many of the tests in Part III, Real-World Test-Driven Development in JavaScript,
determine success by asserting that certain objects have a specific state after some
function was called. As an example, consider Listing 16.2 from Chapter 15, TDD
and DOM Manipulation: The Chat Client, which expects the user form controller
to set the currentUser property of the model object. It passes a dummy model
object to the controller, and then inspects the object's currentUser object to
verify its behavior.
Listing 16.2 Inspecting an object's state to verify test
"test should set model.currentUser": function () {
var model = {};
var event = { preventDefault: stubFn() };
var input = this.element.getElementsByTagName("input")[0];
input.value = "cjno";
assertEquals("cjno", model.currentUser);
The fact that the last line inspects a property of an object passed to the system
under test to verify its success is called state verification . State verification leads to
intuitive tests that clearly describe the outcome of using some part of the system. In
this case, if the input field contains a username when the controller handles a submit
event, we expect it to transfer this username to the model object's currentUser
property. The test does not say anything about how this should happen, thus it is
completely detached from the implementation of handleSubmit .
16.2.2 Behavior Verification
In many cases, testing the direct output of a test is not as simple as in Listing 16.2.
For instance, keeping with the chat client example, the message form controller is
in charge of publishing messages from the client to the server through the model
object. Because there is no server in the tests, we cannot simply ask it for the message
Search WWH ::

Custom Search