Mohamed Ahmed
Mohamed Ahmed

Reputation: 35

sinon stub a method that was executed in then call of promise

describe("/test" , ()=> {

    // separate class 2
    class2 = {

        // function that i wanna stub
    hi: function () {
        return "hi";
    }
    }

// separate class 1
    class1 = {

        // function that i have stubbed and tested
         method1: function() {
           return new Promise((resolve, reject) => {
               resolve(num);

            })
        }

    }

    // method that i will execute
    var parent= function (){

        class1.method1().then(()=>{

            class2.hi();

        })

    }

    // the test
    it("should stub hi method",()=>{


        var hiTest = sinon.stub(class2, 'hi').resolves(5);
        var method1Test = sinon.stub(class1 , 'method1').resolves(5);

     // this start the execution of the promise with then call
        parent();

        // this works fine and test pass
        expect(method1Test.calledOnce.should.be.true);

        // this doesn't work although i executed the function
        expect(hiTest.calledOnce.should.be.true);

    })

})

what i wanna do is test the hi method correctly .. because when i test if the method is executed once or not

although i executed it in the then call of the promise it doesn't show that and it make the calledOnce test fail

Upvotes: 0

Views: 1353

Answers (1)

Tristan Hessell
Tristan Hessell

Reputation: 950

The problem here is that you are testing the code as if it is synchronous, when it is not (as you are using Promise).


To be able to test this properly we need to be able to hook onto the promise chain that is started with parentcalling class1.method1.

We can do this by returning the promise that calling class1.method1 returns.

In terms of the test itself, we need to make sure Mocha doesnt end the test while we are waiting for the promises, so we use the done callback parameter to tell Mocha when we think the test is finished.


describe("/test", ()=> {
  class2 = {
    hi: function () {
      return "hi";
    }
  }

  class1 = {
    method1: function() {
      return new Promise((resolve, reject) => {
        resolve(num);
      })
    }
  }

  var parent = function (){
    return class1.method1().then(()=>{
      class2.hi();
    })
  }

  it("should stub hi method", (done)=> {
    var hiTest = sinon.stub(class2, 'hi').returns(5);
    var method1Test = sinon.stub(class1 , 'method1').resolves(5);

    parent().then(() => {
      expect(method1Test.calledOnce.should.be.true);
      expect(hiTest.calledOnce.should.be.true);
      done();
    });
  })
})

Upvotes: 2

Related Questions