HTML and CSS Reference
In-Depth Information
11.2 Adding Observers
We will kick off the project by implementing a means to add observers to an object.
Doing so will take us through writing the first test, watching it fail, passing it in the
dirtiest possible way, and finally refactoring it into something more sensible.
11.2.1 The First Test
To keep us going through the initial stages of developing the observable library,
we will keep to the Java parallel. This means that the first test will create an ob-
servable object with the Observable constructor and add an observer by calling
the addObserver method on it. To verify that this works, we will be blunt and
assume that Observable stores its observers in an array, and check that the ob-
server is the only item in that array. The test can be seen in Listing 11.3. Save it in
test/observable _ test.js .
Listing 11.3 Expecting addObserver to add observer to internal array
TestCase("ObservableAddObserverTest", {
"test should store function": function () {
var observable = new tddjs.util.Observable();
var observer = function () {};
observable.addObserver(observer);
assertEquals(observer, observable.observers[0]);
}
});
11.2.1.1 Running the Test and Watching it Fail
At first glance the results of running our very first test, in Listing 11.4, is devastating.
Listing 11.4 Running the test
chris@laptop:~/projects/observable $ jstestdriver --tests all
E
Total 1 tests (Passed: 0; Fails: 0; Errors: 1) (0.00 ms)
Firefox 3.6.3 Linux: Run 1 tests \
(Passed: 0; Fails: 0; Errors 1) (0.00 ms)
Observable.addObserver.test \
should store function error (1.00 ms): \
 
 
Search WWH ::




Custom Search