HTML and CSS Reference
In-Depth Information
Listing 11.11 Hard-coding the array
function addObserver(observer) {
this.observers = [observer];
}
Success! As Listing 11.12 shows, the test now passes.
Listing 11.12 Test passing
chris@laptop:~/projects/observable $ jstestdriver --tests all
.
Total 1 tests \
(Passed: 1; Fails: 0; Errors: 0) (0.00 ms)
Firefox 3.6.3 Linux: Run 1 tests \
(Passed: 1; Fails: 0; Errors 0) (0.00 ms)
11.2.2 Refactoring
While developing the current solution, we have taken the quickest possible route
to a passing test. Now that the bar is green, we can review the solution and perform
any refactoring we deem necessary. The only rule in this last step is to keep the bar
green. This means we will have to refactor in tiny steps as well, making sure we
don't accidentally break anything.
The current implementation has two issues we should deal with. The test makes
detailed assumptions about the implementation of Observable and the addOb-
server implementation is hard-coded to our test.
We will address the hard-coding first. To expose the hard-coded solution,
Listing 11.13 augments the test to make it add two observers instead of one.
Listing 11.13 Exposing the hard-coded solution
"test should store function": function () {
var observable = new tddjs.util.Observable();
var observers = [function () {}, function () {}];
observable.addObserver(observers[0]);
observable.addObserver(observers[1]);
assertEquals(observers, observable.observers);
}
 
Search WWH ::




Custom Search