HTML and CSS Reference
In-Depth Information
13.4.3.1 Adding
ajax.cometClient.dispatch
The first test for dispatching data is simply asserting that a method exists,
as Listing 13.45 shows.
Listing 13.45
Expecting
dispatch
to exist
"test should have dispatch method": function () {
var client = Object.create(ajax.cometClient);
assertFunction(client.dispatch);
}
This test fails, so Listing 13.46 adds it in.
Listing 13.46
Adding the
dispatch
method
function dispatch() {
}
ajax.cometClient = {
dispatch: dispatch
};
13.4.3.2 Delegating Data
Next, we're going to feed
dispatch
an object, and make sure it pushes data out
to observers. However, we haven't written
observe
yet, which means that if we
now write a test that requires both methods to work correctly, we're in trouble
if either fail. Failing unit tests should give a clear indication of where a problem
occurred, and using two methods to verify each other's behavior is not a good idea
when none of them exist. Instead, we will leverage the fact that we're going to use
observable
to implement both of these. Listing 13.47 expects
dispatch
to call
notify
on the observable
observers
object.
Listing 13.47
Expecting
dispatch
to
notify
"test dispatch should notify observers": function () {
var client = Object.create(ajax.cometClient);
client.observers = { notify: stubFn() };
client.dispatch({ someEvent: [{ id: 1234 }] });
var args = client.observers.notify.args;