HTML and CSS Reference
In-Depth Information
14.4.1.1 Returning a Promise
We will start refactoring by introducing a new test, one that expects addMessage
to return a promise object, seen in Listing 14.46.
Listing 14.46 Expecting addMessage to return a promise
testCase(exports, "chatRoom.addMessage", {
/* ... */
"should return a promise": function (test) {
var result = this.room.addMessage("cjno", "message");
test.isObject(result);
test.isFunction(result.then);
test.done();
}
});
Notice that I assume you've solved the exercise from before; the test case
should now be using a setup method to create a chatRoom object, available in
this.room .
The test fails as the method is currently not returning an object. We'll fix that
by returning an empty promise object, as in Listing 14.47.
Listing 14.47 Returning an empty promise object
require("function-bind");
var Promise = require("node-promise/promise").Promise;
var id = 0;
var chatRoom = {
addMessage: function (user, message, callback) {
process.nextTick(function () {
/* ... */
}.bind(this));
return new Promise();
},
/* ... */
};
 
Search WWH ::




Custom Search