node.js - stub a function in Nodejs -


i have model stores data db calls store method passing id created

 //db.poll.store method     store: function (opt, db, callback) {             var polldata = {                 question: opt.data.question             };              db.poll.create(polldata).success(function (poll) {                 opt.data.pollid = poll.id;                 models.polloption.store(opt, db, callback);             }).error(function(error) {                 callback(error, null);             });         } 

i want have unit test make sure valid opt.data.pollid passed models.polloption.store() method , don't want check if models.polloption.store() behaviour correctly or not, mocked/overwritten models.polloption.store method. unit test below

describe('method store', function () {     it('should able create poll , pass poll.id polloption.store', function (done) {         var opt = {             data: {                 question: "do love nodejs?"             }         };          var temp = db.polloption.store;         //mock function         db.polloption.store = function (opt, db, callback) {             expect(opt.data.pollid).not.to.be.empty             //restore before             db.polloption.store = temp;             callback();         };          db.poll.store(opt, db, done);     }); }); 

can achieve using sinon.js , stub db.polloption.store method?

i used test framework, , since dont have full code, here example of how use sinon.spy

basically, eq(1, opt) means expect(opt).equal.to(1)

var method = { store : function(a){   return a+1 }}  tests({    'should able create poll , pass poll.id polloption.store': function () {       sinon.spy(method, "store")              // create spy       method.store({opt:1})                   // call function       var spycall = method.store.getcall(0)   // sinon spy api .getcall(0)                                               // assume [objects]                                                // created spy on calling method()       eq(1, spycall.args[0].opt)              // expect()     } }); 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -