Skip to content

Instantly share code, notes, and snippets.

@foxbunny
Created July 22, 2011 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foxbunny/1100451 to your computer and use it in GitHub Desktop.
Save foxbunny/1100451 to your computer and use it in GitHub Desktop.
Problematic fixture module
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) {
series.push(function addDocument(callback) {
var document = new Model(fixture);
document.save(function(err, doc) {
callback(err);
});
});
});
series.push(function checkDocuments(callback) {
Model.find({}, function(err, docs) {
assert.equal(docs.length, fixtures.length,
'Number of loaded docs incorrect: expected ' + fixtures.length + ' got ' + docs.length);
callback(err);
});
});
}
series.push(function performTest(callback) {
perform(callback);
});
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