Skip to content

Instantly share code, notes, and snippets.

@fwielstra
Created June 14, 2011 15:12
Show Gist options
  • Save fwielstra/1025099 to your computer and use it in GitHub Desktop.
Save fwielstra/1025099 to your computer and use it in GitHub Desktop.
An example unit test for our save thread method
var nodeunit = require('nodeunit');
exports['API'] = nodeunit.testCase({
'The show() API method returns a specific thread by title and its replies by threadid': function(test) {
test.expect(4);
// our mock data
var posts = [{post: 'test'}, {post: 'test2'}];
var thread = {_id: '1234', title: 'my thread'};
// A mock thread that overrides the findOne method, checking the parameters
// and calling the given callback with our mock thread object.
var MockThread = {
findOne: function(params, callback) {
test.deepEqual(params, {title: thread.title});
callback(null, thread);
}
};
// A mock post that check if it was called with the right parameters,
// returns our mock posts.
var MockPost = {
find: function(params, callback) {
test.equal(params.thread, thread._id);
callback(null, posts);
}
};
// Our request parameters.
var request = {
params: {
title: thread.title
}
};
// Finally, our response object in which we test to make sure our
// controller returns the right results.
var response = {
send: function(params) {
test.equal(params[0].thread, thread);
test.equal(params[0].posts, posts);
}
}
// Phew. Run it. Note we pass our mock Thread and Post to the API here.
var api = require('./controllers/api.js')(MockThread, MockPost);
api.show(request, response);
test.done();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment