HTML and CSS Reference
In-Depth Information
We expect this test to fail in the face of a missing hasObserver , which it
does. Making the Test Pass
Listing 11.16 shows the simplest solution that could possibly pass the current test.
Listing 11.16 Hard-coding hasObserver 's response
function hasObserver(observer) {
return true;
Observable.prototype.hasObserver = hasObserver;
Even though we know this won't solve our problems in the long run, it keeps
the tests green. Trying to review and refactor leaves us empty-handed as there are no
obvious points where we can improve. The tests are our requirements, and currently
they only require hasObserver to return true . Listing 11.17 introduces another
test that expects hasObserver to return false for a non-existent observer, which
can help force the real solution.
Listing 11.17 Expecting hasObserver to return false for non-existent observers
"test should return false when no observers": function () {
var observable = new tddjs.util.Observable();
assertFalse(observable.hasObserver(function () {}));
This test fails miserably, given that hasObserver always returns true , forcing
us to produce the real implementation. Checking if an observer is registered is a
simple matter of checking that the this.observers array contains the object
originally passed to addObserver as Listing 11.18 does.
Listing 11.18 Actually checking for observer
function hasObserver(observer) {
return this.observers.indexOf(observer) >= 0;
Search WWH ::

Custom Search