HTML and CSS Reference
In-Depth Information
Unfortunately, this doesn't play out exactly as planned. The previous test, which
also calls post , is now attempting to call addMessage on chatRoom , which is
undefined in that test. We can fix the issue by moving the chatRoom stub into
setUp as Listing 14.18 does.
Listing 14.18 Sharing the chatRoom stub
function controllerSetUp() {
/* ... */
this.controller.chatRoom = { addMessage: stub() };
}
All the tests go back to a soothing green, and we can turn our attention to
the duplicated logic we just introduced in the second test. In particular, both tests
simulates sending a request with a body. We can simplify the tests considerably by
extracting this logic into the setup. Listing 14.19 shows the updated tests.
Listing 14.19 Cleaning up post tests
function controllerSetUp() {
/* ... */
this.sendRequest = function (data) {
var str = encodeURI(JSON.stringify(data));
this.req.emit("data", str.substring(0, str.length / 2));
this.req.emit("data", str.substring(str.length / 2));
this.req.emit("end");
};
}
testCase(exports, "chatRoomController.post", {
/* ... */
"should parse request body as JSON": function (test) {
var data = { data: { user: "cjno", message: "hi" } };
JSON.parse = stub(data);
this.controller.post();
this.sendRequest(data);
test.equals(JSON.parse.args[0], JSON.stringify(data));
test.done();
},
/* ... */
});
 
Search WWH ::




Custom Search