Skip to content

Instantly share code, notes, and snippets.

Created June 14, 2011 15:12
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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) {
// 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);, response);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment