HTML and CSS Reference
In-Depth Information
Listing 14.36 Expecting addMessage to pass the created message
"should call callback with new object": function (test) {
var txt = "Some message";
this.room.addMessage("cjno", txt, function (err, msg) {
test.isObject(msg);
test.isNumber(msg.id);
test.equals(msg.message, txt);
test.equals(msg.user, "cjno");
test.done();
});
}
Listing 14.37 shows an attempt at passing the test. It calls the callback with an
object and cheats the id by hard-coding it to 1.
Listing 14.37 Passing the object to the callback
addMessage: function (user, message, callback) {
/* ... */
var data;
if (!err) {
data={id:1,user: user, message: message };
}
if (typeof callback == "function") {
callback(err, data);
}
}
With this in place, the tests are back to green. Next up, the id should be unique
for every message. Listing 14.38 shows the test.
Listing 14.38 Expecting unique message ids
"should assign unique ids to messages": function (test) {
var user = "cjno";
this.room.addMessage(user, "a", function (err, msg1) {
this.room.addMessage(user, "b", function (err, msg2) {
test.notEquals(msg1.id, msg2.id);
test.done();
});
}.bind(this));
}
 
Search WWH ::




Custom Search