HTML and CSS Reference
In-Depth Information
Running the test produces some disappointing results. The test passes, but
unfortunately the previous test now breaks, as there is no
addEventHandler
method around at the point of running it. We can fix this and the duplicated test
code by elevating some common code into a
setUp
method, as Listing 15.11
shows.
Listing 15.11
Extracting code into
setUp
/* ... */
var dom = tddjs.namespace("dom");
/* ... */
TestCase("UserFormControllerSetViewTest", {
setUp: function () {
this.controller = Object.create(userController);
this.element = {};
dom.addEventHandler = stubFn();
},
"test should add js-chat class": function () {
this.controller.setView(this.element);
assertClassName("js-chat", this.element);
},
"test should handle submit event": function () {
this.controller.setView(this.element);
assert(dom.addEventHandler.called);
assertSame(this.element, dom.addEventHandler.args[0]);
assertEquals("submit", dom.addEventHandler.args[1]);
assertFunction(dom.addEventHandler.args[2]);
}
});
Even though both tests use
setView
in the same way, we keep it out of
setUp
,
because this call is not part of the setup, rather it is the
exercise
step of the test.
Refactoring the test got the tests back on track, and they now both pass.
For the next test, we need to verify that the event handler is bound to the
controller object. To achieve this we need
stubFn
to record the value of
this
at
call time. Listing 15.12 shows the updated function.