Created
June 14, 2011 15:12
-
-
Save fwielstra/1025099 to your computer and use it in GitHub Desktop.
An example unit test for our save thread method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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