Created
July 23, 2011 10:39
-
-
Save foxbunny/1101295 to your computer and use it in GitHub Desktop.
Broken fixture module
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 async = require('async'), | |
assert = require('assert'); | |
// Clean up database | |
function cleanUp(Model, callback) { | |
Model.remove({}, function(err) { | |
Model.find({}, function(err, docs) { | |
assert.equal(docs.length, 0, 'Documents should not exist, found ' + | |
docs.length); | |
callback(err); | |
}); | |
}); | |
} | |
// Test in a clean database | |
function inClean(Model, fixtures, perform) { | |
var series = [ | |
function initialCleanup(callback) { | |
cleanUp(Model, callback); | |
} | |
]; | |
if (fixtures && fixtures.length) { | |
fixtures.forEach(function(fixture) { | |
console.log('loading fixture'); | |
series.push(function addDocument(callback) { | |
var document = new Model(fixture); | |
document.save(function(err, doc) { | |
callback(err); | |
}); | |
}); | |
}); | |
} | |
series.push(function performTest(callback) { | |
perform(function(err) { | |
if (err) { | |
console.log(err); | |
} | |
callback(err); | |
}); | |
}); | |
series.push(function finalCleanup(callback) { | |
cleanUp(Model, callback); | |
}); | |
async.series(series); | |
} | |
exports.cleanUp = cleanUp; | |
exports.inClean = inClean; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment