Created
July 5, 2013 01:10
-
-
Save johnnyhalife/5931047 to your computer and use it in GitHub Desktop.
get db es la papa
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
// ./lib/controllers/user.js | |
var getDb = require('mongo-getdb'); | |
var controller = module.exports; | |
controller.getUserById = function(id, callback) { | |
var criteria = { username: id, deleted: { $ne: true } }; | |
this.getUserByCriteria(criteria, callback); | |
}; | |
controller.getUserByCriteria = function(criteria, callback) { | |
this.withDb(function(db){ | |
db.collection('profiles').findOne(criteria, callback); | |
}); | |
}; | |
controller.withDb = function(callback) { | |
getDb(callback); | |
}; | |
// el test con sinon | |
var should = require('should'), | |
sinon = require('sinon'), | |
controller = require('../../../lib/controllers/user.js'); | |
describe("User Controller (new version)", function() { | |
it("should create the criteria for searching by username", function(done) { | |
var getUserByCriteria = sinon.stub(controller, "getUserByCriteria"); | |
getUserByCriteria.callsArgWith(1, undefined, { username: 'johnnyhalife' } ); | |
controller.getUserById('johnnyhalife', function(err, user) { | |
user.username.should.equal('johnnyhalife'); | |
getUserByCriteria.called.should.equal(true); | |
controller.getUserByCriteria.restore(); | |
done(); | |
}); | |
}); | |
it("should look for the user with a given criteria", function(done) { | |
var result = { username: 'johnnyhalife' }; | |
var criteria = { username: 'johnnyhalife', deleted: { $ne : true } }; | |
var collection = sinon.mock({ findOne: function(){ } }); | |
collection.expects('findOne').withArgs(criteria).callsArgWith(1, undefined, result); | |
var db = sinon.mock({ collection: function(){ } }); | |
db.expects('collection').withArgs('profiles').returns(collection.object); | |
// stub the database | |
var withDb = sinon.stub(controller, "withDb"); | |
withDb.callsArgWith(0, db.object); | |
controller.getUserByCriteria(criteria, function(err, user) { | |
[db, collection].forEach(function(d) { d.verify(); }); | |
result.should.equal(user); | |
withDb.called.should.equal(true); | |
controller.withDb.restore(); | |
done(); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment