Search code examples
javascriptunit-testingsinonstub

Sinon stub is not working in this scenario?


I am trying to use sinon stub to test my function by creating mock values for IF statement is working as it is described in the testFunction

In one of the file myFunction.js I have functions like

function testFunction() {
  var job = this.win.get.value1   //test
  var job1 = this.win.get.value2 // test1
  if(job === 'test' && job1 === 'test1') {
    return true;
  }
    return false; 
}

and I am trying to test testFunction using karma and I tried to stub test function using sinon stub like this

it('should test my function', function(done) {
  var stub = sinon.stub(myFunction,'job','job1').returns('test','test1');
  myFunction.testFunction('test', function(err, decodedPayload) {
    decodedPayload.should.equal(true);
    done();
  });
});

Can anybody tell me where i am doing the mistake?


Solution

  • You are using sinon.stub incorrectly. You need to make two calls to sinon.stub, one for each method of myFunction that you want to stub.

    it('should test my function', function(done) {
      sinon.stub(myFunction,'job').returns('test');
      sinon.stub(myFunction,'job1').returns('test1');
      myFunction.testFunction('test', function(err, decodedPayload) {
        decodedPayload.should.equal(true);
        done();
      });
    });
    

    See Sinon documentation