HTML and CSS Reference
In-Depth Information
var controller = chatRoomController.create(req, res);
test.inherits(controller, chatRoomController);
test.strictEqual(controller.request, req);
test.strictEqual(controller.response, res);
test.done();
}
});
Notice that Node's assertions flip the order of the arguments compared with
what we're used to with JsTestDriver. Here, the order is
actual, expected
rather than the usual
expected, actual
. This is an important detail to get
right, as failure messages will suffer if we don't.
As V8 implements parts of ECMAScript5, we can pass this test by using
Object.create
, as Listing 14.9 shows.
Listing 14.9
Creating controllers
var chatRoomController = {
create: function (request, response) {
return Object.create(this, {
request: { value: request },
response: { value: response }
});
}
};
The test passes. Defining
request
and
response
this way means that their
enumerable
,
configurable
and
writable
attributes are set to the default
value, which in all cases is
false
. But you don't need to trust me, you can test it using
test.isWritable
,
test.isConfigurable
and
test.isEnumerable
,
or their counterparts,
test.isNot*
.
14.2.4 Adding Messages on POST
The
post
action accepts JSON in the format sent by
cometClient
from
Chapter 13,
Streaming Data with Ajax and Comet,
and creates messages. If your
memory's a bit rusty on the JSON format, a sample request to create a message can
be seen in Listing 14.10.