HTML and CSS Reference
In-Depth Information
Save the test in test/chapp/chat _ room _ controller _ test.js and
run it with ./run _ tests . It fails horribly with an exception stating that Node
“Can't find module chapp/chat room controller.” Save the contents of Listing 14.6
in lib/chapp/chat _ room _ controller.js to resolve the issue.
Listing 14.6 Creating the controller module
var chatRoomController = {
create: function () {}
module.exports = chatRoomController;
Running the tests again should produce more uplifting output along the lines
of Listing 14.7.
Listing 14.7 First successful test
chris@laptop:~/projects/chapp $ ./run_tests
chatRoomController should be object
OK: 2 assertions (2ms)
Note how the test case receives a test object and calls its done method.
Nodeunit runs tests asynchronously, so we need to let it know explicitly when a test
is done. In Part I, Test-Driven Development, I argued that unit tests rarely need to be
asynchronous. For Node the situation is a little bit different, because not allowing
asynchronous tests would basically mean having to stub or mock every system call,
which simply is not a viable option. Doing so would make testing challenging, and
without proper interface enforcement, error-prone.
14.2.3 Creating a Controller
Listing 14.8 creates a controller and asserts that it has request and response
properties corresponding to the arguments we pass the create method.
Listing 14.8 Test creating new controllers
testCase(exports, "chatRoomController.create", {
"should return object with request and response":
function (test) {
var req = {};
var res = {};
Search WWH ::

Custom Search