javascript - How to spy on a class method in node-jasmine? -


i have module user - this:

module.exports = user = (function() {    function user(params) {   this.id               = params.id;   this.first_name       = params.first_name || '';   this.last_name        = params.last_name  || '';   this.username         = params.username;   this.email            = params.email;   this.password         = params.password;   };    user.findbyusername = function(username, callback) {     if (!_.isempty(username)) {       var opts = {table: table, where: {sql:  "username='"+username+"'"}};       querydispatcher.findwhere(opts, function(err, result) {         if(!_.isempty(err)) { return callback(err, null)}           callback(null, result.rows[0]);       });     };   };  return user; }; 

the function uses class method:

module.exports = authstrategies = (function() {    authstrategies.localstrategy = function(username, password, done) {     async.waterfall([       function(callback) {         user.findbyusername(username, function(err, user){           if (err) { callback(err) };           if (_.isempty(user)) {             callback(null, false, { message: 'incorrect username.' });           };           callback(null, user, null)         });       },        function(user, opts, callback) {         "do here , call next callback"       }]        , function(err, user, opts) {         if(err) { return done(err)}         if(!user) { return done(null, false, opts.message)}          done(null, user)       });   };    return authstrategies; })(); 

i have jasmine test -

var auth = require('path authstrategies module')  describe('auth', function() {    describe('#authstrategies.localstrategy', function() {     describe('when user creds valid', function() {       var test_user;       beforeeach(function(){         test_user = new user({           username: 'test996'           , password: 'password123'           , email: 'testemamil@email.com'           , first_name: ''           , last_name: ''         });         spyon(user, "findbyusername").and.callfake(function(usrename, cb) {           cb(null, test_user);         });       });        it('returns user object', function(done) {         auth.localstrategy('test996', 'password123', function(err, user, opts) {           expect(err).toequal(null);           expect(user).toequal(test_user);           done()         })       });     });   }); }); 

essentially want stub out user class method findbyusername , fake callback own results i.e nul error , user(as if find successfully).

i have spy on many "class" methods in app , don't have problem. baffling me bit. error shows when add .and.callthrough or .and.callfake spy.. moment remove test times out ...which makes sense spy works , doesn't call callback needed async waterfall continue.

the error getting -

error message

so figured out -

the error see above happens anyway. reason getting above "extra info" throwing me off btw - because running test separately.

./node_modules/.bin/jasmine ./tests_to_run_spec.js

what normal happen - timeout failure due missing callback. in case above wasn't calling callback in faked function sup[plied properly.

actually more interestingly - running jasmine-node path doesn't .and.... being called on particular spy. have no idea. how got spyon(user, 'findbyusername').and.callfake ... work


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -