mock method calling callback function sinon

Stanley picture Stanley · May 24, 2017 · Viewed 9.4k times · Source

How do you mock an outer method calling a callback using sinon? Example given the following code, getText should return 'a string' as response in the callback function

sinon.stub(a, 'getText').returns('a string')
let cb = function(err, response) {
   console.log(response)
}
a.getText('abc', cb)

and it should produce the output 'a string' since it calls callback function cb but there is no output

Answer

robertklep picture robertklep · May 24, 2017
sinon.stub(a, 'getText').yields(null, 'a string');

yields() will call the first function argument that gets passed to the stubbed function with the arguments provided (null, 'a string').