HTML and CSS Reference
In-Depth Information
Listing 14.15
Sharing setup
function controllerSetUp() {
var req = this.req = new EventEmitter();
var res = this.res = {};
this.controller = chatRoomController.create(req, res);
this.jsonParse = JSON.parse;
}
function controllerTearDown() {
JSON.parse = this.jsonParse;
}
/* ... */
testCase(exports, "chatRoomController.create", {
setUp: controllerSetUp,
/* ... */
});
testCase(exports, "chatRoomController.post", {
setUp: controllerSetUp,
tearDown: controllerTearDown,
/* ... */
});
With this change the tests should refer to
controller
,
req
and
res
as
properties of
this
.
14.2.4.2 Extracting the Message
With the request body readily parsed as JSON, we need to extract the message
from the resulting object and pass it somewhere it will be kept safe. As we're going
through this exercise top-down, we don't have a data model yet. We will have to
decide roughly what it's going to look like, and stub it while we finish the
post
method.
Messages should belong to a chat room. As the chat room needs to persist
between requests, the controller will depend on the server assigning it a
chatRoom
object, on which it can call
addMessage(user, message)
.
The test in Listing 14.16 verifies that
post
passes data to
addMessage
according to this interface.