development:testing:sinonjs:start

This is an old revision of the document!


Sinon JS

  • Check if a method is called
const spy = sinon.spy(object, 'method')
...
spy.restore() // remove spy
expect(spy.callCount).to.be.eql(n) // check if the method is call n times
  • Replace existing method with stub to avoid actually execute the method
function fake() {  // a fake function to be called
}

const stub = sinon.stub(object, 'method').callFakes(fake) // you could also replace the fake with another real function
...
stub.restore() // remove stub
expect(stub.callCount).to.be.eql(n) // check if the method is call n times
  • development/testing/sinonjs/start.1547355446.txt.gz
  • Last modified: 2019/01/13 12:57
  • by don.lee