jeudi 28 juillet 2016

"this" reference is not working in nodeJs

I have two methods in my nodeJs code like

function method1(id,callback){
  var data = method2();
  callback(null,data);
}

function method2(){
  return xxx;
}

module.exports.method1 = method1;
module.exports.method2 = method2;

for testing function method1 using Sinon and Mocha i had to stub method method2. For which it required to call method method2 as

function method1(id,callback){
      var data = method2();
      callback(null,data);
}

Test Code for this

describe('test method method2', function (id) {
    var id = 10;
    it('Should xxxx xxxx ',sinon.test(function(done){
       var stubmethod2 = this.stub(filex,"method2").returns(data);
       filex.method1(id,function(err,response){
         done();
       })
    })
})

using this test cases passed, But the code stopped working with error this.method2 is not a function.

Is there any way i can get rid of this or module.exports which seems buggy.

Please let me know if i missed any other info..

Aucun commentaire:

Enregistrer un commentaire