HTML and CSS Reference
In-Depth Information
assert(model.notify.called);
assertEquals("message", model.notify.args[0]);
assertObject(model.notify.args[1]);
}
});
Rather than performing state verification on the model object to verify that it
received the given message, we stub the notify method and use behavior veri-
fication to verify that it was called correctly. Tests for the cometClient verify
that calling the method correctly will make sure the message is correctly sent to the
server.
17.2.3.2 Risks Introduced by Mocks and Stubs
In dynamic languages such as JavaScript, there is always a risk associated with test
doubles. As an example, consider the test in Listing 17.12, which verifies that the
form is not actually submitted when the user submits a message to the chat service.
Listing 17.12 Verifying that the form submit action is aborted
"test should prevent event default action": function () {
this.controller.handleSubmit(this.event);
assert(this.event.prevenDefault.called);
}
Having written this test, we have introduced a new requirement for the system
under test. After confirming that it fails, we proceed to write the passing code. Once
the test passes, we move on to the next behavior. Upon testing the resulting code in
a browser, we will be shocked to find that the code throws an error when posting a
message.
The observant reader will already have noticed the problem; we accidentally
misspelled preventDefault , leaving out the first “t.” Because the stub is in no
way associated with a real exemplar of the kind of object we are faking, we have no
safety net catching these kinds of errors for us. Languages like Java solve these kinds
of problems with interfaces. Had the event stub been stated to implement the event
interface, we would have realized our mistake, as the test would err because the stub
did not implement preventDefault . Even if it did—e.g., through inheritance—
the call to prevenDefault from production code would have erred because this
method definitely isn't part of the event interface.
Introducing typos in method names may seem like a silly example, but it's a
simple illustration of a problem that can take a lot more obscure forms. In the
Search WWH ::




Custom Search