HTML and CSS Reference
In-Depth Information
an in-memory file system. The test setup can then make sure to place the fake
implementation ahead of the built-in one on the load path. Neither individual tests
nor production code will be aware that require("fs") actually loads a simplified
in-memory file system.
16.1.3 Dummy Object
A dummy object, as its name suggests, is usually just an empty object or function.
When testing functions that expect several parameters, we are often only concerned
with one of them at a time. If the function we're testing throws errors for missing
or wrongly typed arguments, we can pass it a dummy to “shut it up” while we focus
on behavior not related to the argument in question.
As an example, consider the test in Listing 16.1 from Chapter 15, TDDandDOM
Manipulation: The Chat Client. The test verifies that the message list controller sets
the element's scrollTop equal to the value of its scrollHeight . However,
the method also appends a new DOM element to the view element, and throws an
exception if it does not have an appendChild method. For the purpose of this
test we use a dummy to pass the test on appendChild to get to the behavior we
want to test.
Listing 16.1 Using a dummy function
"test should scroll element down": function () {
var element = {
appendChild: stubFn(),
scrollHeight: 1900
};
this.controller.setView(element);
this.controller.addMessage({ user:"me",message:"Hey" });
assertEquals(1900, element.scrollTop);
}
16.2 Test Verification
Unit tests have four stages; setup , often divided between a shared setUp method
and test specific configuration of objects; exercise , in which we call the function(s)
to test; verification , in which we assert that the result of the exercise stage coincides
with our expectations; and finally tear down , which never happens inside a test, but
rather in a dedicated and shared tearDown method.
 
 
Search WWH ::




Custom Search