For writing my test I am using mocha framework in a stack with Chai as an assertion library and Sinon.JS for mocks, stubs and spies. Assuming that I have some chained functions, for example:
request
.get(url)
.on('error', (error) => {
console.log(error);
})
.on('response', (res) => {
console.log(res);
})
.pipe(fs.createWriteStream('log.txt'));
What is the best way to stub them, considering that I would like to assert their calling with needed arguments?
Such a construction:
requestStub = {
get: function() {
return this;
},
on: function() {
return this;
}
//...
};
Would not permit me to assert those methods like:
expect(requestStub.get).to.be.called;
expect(requestStub.on).to.be.calledWith('a', 'b');
The usage of returns()
method of a stub:
requestStub = {
get: sinon.stub().returns(this),
on: sinon.stub().returns(this),
};
Will not return the object, and cause an error:
TypeError: Cannot call method 'on' of undefined
Tell me please, how can I stub chained functions?
Aucun commentaire:
Enregistrer un commentaire