Skip to content

Instantly share code, notes, and snippets.

@oveddan
Created July 25, 2012 22:21
Show Gist options
  • Save oveddan/3179069 to your computer and use it in GitHub Desktop.
Save oveddan/3179069 to your computer and use it in GitHub Desktop.
TDD callback testing sample
var fs = require('fs');
var logReader = {
getLogEntries : function(logDate, callback){
if(!(logDate instanceof Date))
throw new Error('date must be of type Date');
var logFileName = logDate.toDateString() + '.log';
fs.readFile(logFileName, function(err, data){
if(err)
callback(err);
else
callback(null, data.split('\n'));
});
}
};
module.exports = logReader;
var logReader = require('./logReader');
var should = require('should')
, sinon = require('sinon')
, fs = require('fs');
describe('logReader', function(){
describe('getLogEntries(logDate, callback)', function(){
beforeEach(function(){
// spy out fs method that reads files
this.oldReadFile = fs.readFile;
fs.readFile = sinon.spy();
});
afterEach(function(){
// restore readFile method
fs.readFile = this.oldReadFile;
});
it('should callback with error if date is not a Date', function(){
var date = 123213;
(function(){
logReader.getLogEntries(date)
}).should.throw('date must be of type Date');
});
it('should read log file with name of date converted to date string', function(){
// SETUP
var logDate = new Date();
var logFileName = logDate.toDateString() + '.log';
// TEST
logReader.getLogEntries(logDate, function(){});
// SHOULD
fs.readFile.calledOnce.should.be.ok;
fs.readFile.firstCall.args[0].should.eql(logFileName);
fs.readFile.firstCall.args[1].should.be.a('function');
});
it('should callback with error if reading log file causes error', function(done){
// SETUP
var logDate = new Date();
var expectedError = new Error('File does not exist');
var callback = function(err, data){
// SHOULD
err.should.eql(expectedError);
done();
};
logReader.getLogEntries(logDate, callback);
// get first call from readFile, and second argument is the callback. invoke it with expected error
var readFileCallback = fs.readFile.firstCall.args[1];
// TEST
readFileCallback(expectedError);
});
it('should callback with array of log entries from log file contents split by newline', function(done){
var expectedFileData = "lorum ipsum\ndolurm pidem flummmox\njamarnics\nsdfasdf",
expectedOutput = ["lorum ipsum","dolurm pidem flummmox","jamarnics","sdfasdf"];
// SETUP
var logDate = new Date();
var callback = function(err, data){
// SHOULD
data.should.eql(expectedOutput);
done();
};
logReader.getLogEntries(logDate, callback);
// get first call from readFile, and second argument is the callback. invoke it with file contents
var readFileCallback = fs.readFile.firstCall.args[1];
// TEST
readFileCallback(null, expectedFileData);
});
})
});
test:
@./node_modules/.bin/mocha -R spec $(shell find logReader_tests.js)
.PHONY: test
{
"name" : "callbackTesting",
"version" : "0.0.1",
"dependencies" : {
},
"devDependencies" : {
"mocha" : "1.0.3",
"sinon" : "1.4.2",
"should" : "0.6.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment