HTML and CSS Reference
In-Depth Information
16.3.1 Stubbing to Avoid Inconvenient Interfaces
Listing 16.4 shows the previous chat client message list controller test again. It uses
a stub in place of a DOM element to verify that the message list controller scrolls
the element all the way down after appending DOM elements to it.
Listing 16.4 Using a stub to avoid the DOM
"test should scroll element down": function () {
var element = {
appendChild: stubFn(),
scrollHeight: 1900
this.controller.addMessage({ user:"me",message:"Hey" });
assertEquals(1900, element.scrollTop);
As noted earlier, the test uses a stub appendChild . Furthermore, it specifies
a scrollHeight with a known value, allowing us to verify that the scrollTop
property was assigned this value. By using a stub we avoid having to render the
element and we avoid calculating the actual scrollTop value, thus making the
test faster and avoiding possible cross browser issues related to the rendering of
the element.
16.3.2 Stubbing to Force Certain Code Paths
Stubs are frequently used to manipulate the system under test to take a specific
path, allowing us to verify a single aspect in isolation. For instance, in Chapter 12,
Abstracting Browser Differences: Ajax, we wrote the test in Listing 16.5 to verify that
local requests are considered successful if they have an HTTP status of “0.”
Listing 16.5 Expecting success for local requests
"test should call success handler for local requests":
function () {
this.xhr.readyState = 4;
this.xhr.status = 0;
var success = stubFn();
tddjs.isLocal = stubFn(true);
ajax.get("file.html", { success: success });
Search WWH ::

Custom Search