HTML and CSS Reference
In-Depth Information
"test should not throw if notify undefined": function () {
this.client.observers = {};
assertNoException(function () {
this.client.dispatch({ someEvent: [{}] });
}.bind(this));
}
});
All the dispatch tests are now grouped inside their own test case. The test
case adds two new tests: one that checks that dispatch can deal with the case
in which there is no observers object, and another in which the observers
object has been tampered with. The latter test is there simply because the object is
public and could possibly be mangled. Both tests fail, so Listing 13.50 hardens the
implementation.
Listing 13.50 Being careful with observers
function dispatch(data) {
var observers = this.observers;
if (!observers
||
typeof observers.notify != "function") {
return;
}
/* ... */
}
Next up, we go a little easier on the assumptions on the data structure the
method receives. Listing 13.51 adds two tests that tries (successfully, for now) to
overthrow dispatch by feeding it bad data.
Listing 13.51 Testing dispatch with bad data
TestCase("CometClientDispatchTest", {
setUp: function () {
this.client = Object.create(ajax.cometClient);
this.client.observers = { notify: stubFn() };
},
/* ... */
"test should not throw if data is not provided":
function () {
 
Search WWH ::




Custom Search