node.js - stubbing a Mongoose model -
i'm trying unit test function:
exports.createtoken = function( user ){ var tokenexpirationdate = moment().add( config.tokenexpiration, 'minutes' ).valueof(), payload = { sub: user.id, exp: tokenexpirationdate }, token = jwt.sign( payload, config.tokensecret ), userinfo = user.tojson(); delete userinfo._id; delete userinfo.__v; delete userinfo.password; return { user: userinfo, token: token, expires: tokenexpirationdate }; };
since idea not have dependencies, have stub mongoose model function receives.
since have little no experience in unit testing first thought in mocha:
describe( '#createtoken', function() { it( 'should return jwt token', function( done ) { var fakeusermodel = { id: '558480eeffa8528c15492361', _id: '558480eeffa8528c15492361', __v: '0', tojson: function(){ return { email: 'foo@bar.org', password: '123456', username: 'foo' } } } var token = auth.createtoken(fakeusermodel); token.should.have.property('user').and.be.an( 'object' ); token.user.should.have.property('email','foo@bar.org').and.be.a('string'); token.user.should.have.property('username','foo').and.be.a('string'); token.should.have.property('token').and.be.a( 'string' ); token.should.have.property('expires').and.be.a( 'string' ); done(); }); });
is there way achieve this, perhaps using sinon/rewire ?
Comments
Post a Comment