jeudi 14 février 2019

stub a function with callback, don't return promise

I have a service which is calling a function, I am writing a test for that service and I need to stub the function inside that service, that function has callback instead of returning a promise. when I make the stub for that I and give dummy return but it hangs as service expects the callback, here is my code for the test

describe('Testing Token Service', () => {
let _stub =null;
beforeEach(async()=>{
    _stub = sinon.stub(tModel.prototype, "save")
})
afterEach(async()=>{
  if(_stub){
    _stub.restore()
  }
})

it('testing function saveToken_mongo() ', (done) => {
    _stub.returns(Promise.resolve( {'status' : 'true'} ))
    token_service.saveToken_mongo({},function(err, data){
        console.log(err, data)
        done();
    })
    // done()
});   });

and here is the service function for which I am writing test

Service.prototype.saveToken_mongo = function(token, callback){
var _token = new tModel( token ) ;
_token.save(function(err, data){
    if(err){
        callback(err, null);
        return ;
    }
    else{
        callback(null, {'status':true})
        return ;
    }  
}) }

I need to make dummy callback return from that function using stub.

Aucun commentaire:

Enregistrer un commentaire