Skip to content

Instantly share code, notes, and snippets.

@gergelyke
Last active August 29, 2015 14:11
Show Gist options
  • Save gergelyke/3dd1fce2bf936c4b1ec0 to your computer and use it in GitHub Desktop.
Save gergelyke/3dd1fce2bf936c4b1ec0 to your computer and use it in GitHub Desktop.
User model
var db = require('db');
// do some init here, or connect
db.init();
var userModel = require('User')({
db: db
});
userModel.create(function (err, user) {
});
var test = require('tape');
var userModel = require('User');
test('it creates a user with id', function (t) {
var user = {
id: 1
};
var fakeDb = {
query: function (done) {
done(null, user);
}
}
userModel({
db: fakeDb
}).create(function (err, user) {
t.equal(user.id, 1, 'User id should match');
t.end();
})
});
function userModel (options) {
var db;
if (!options.db) {
throw new Error('Options.db is required');
}
db = options.db;
return {
create: function (done) {
db.query('INSERT ...', done);
}
}
}
module.exports = userModel;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment