HTML and CSS Reference
In-Depth Information
Listing 15.12
Recording
this
in
stubFn
function stubFn(returnValue) {
var fn = function () {
fn.called = true;
fn.args = arguments;
fn.thisValue = this;
return returnValue;
};
fn.called = false;
return fn;
}
The next test, seen in Listing 15.13, uses the improved
stubFn
to assert that
the event handler is the controller's
handleSubmit
method, readily bound to the
controller object.
Listing 15.13
Expecting the event handler to be
handleSubmit
bound
to controller
"test should handle event with bound handleSubmit":
function () {
var stub = this.controller.handleSubmit = stubFn();
this.controller.setView(this.element);
dom.addEventHandler.args[2]();
assert(stub.called);
assertSame(this.controller, stub.thisValue);
}
This test shows another reason for not elevating the
setView
call to the
setUp
method; here we need additional setup before calling it, to be sure the method
uses the stubbed
handleSubmit
method—not the original one, which would
fail our test indefinitely. Listing 15.14 updates the call to pass the test. Note that
the implementation requires the
bind
implementation from Chapter 6,
Applied
Functions and Closures,
in
lib/function.js
.