HTML and CSS Reference
In-Depth Information
As seen in Listing 11.33, we already have tests that call addObserver and
hasObserver before doing anything else.
Listing 11.33 Tests targeting addObserver and hasObserver
TestCase("ObservableAddObserverTest", {
"test should store functions": function () {
var observable = new tddjs.util.Observable();
var observers = [function () {}, function () {}];
observable.addObserver(observers[0]);
observable.addObserver(observers[1]);
assertTrue(observable.hasObserver(observers[0]));
assertTrue(observable.hasObserver(observers[1]));
},
/* ... */
});
TestCase("ObservableHasObserverTest", {
"test should return false when no observers": function () {
var observable = new tddjs.util.Observable();
assertFalse(observable.hasObserver(function () {}));
}
});
The notifyObservers method however, is only tested after addObserver
has been called. Listing 11.34 adds a test that expects it to be possible to call this
method before adding any observers.
Listing 11.34 Expecting notifyObservers to not fail if called before
addObserver
"test should not fail if no observers": function () {
var observable = new tddjs.util.Observable();
assertNoException(function () {
observable.notifyObservers();
});
}
With this test in place, we can empty the constructor as seen in Listing 11.35.
 
Search WWH ::




Custom Search