admin管理员组文章数量:1354705
If I have a stub for a function that takes 2 callbacks, how can I wire up sinon.js to call both callbacks when the stubbed function is invoked?
For example - here's function that I want to stub which takes 2 functions as arguments:
function stubThisThing(one, two) {
... one and two are functions ...
... contents stubbed by sinon.js ...
}
I can use sinon to call either one of the arguments:
stubbedThing.callsArg(0);
or
stubbedThing.callsArg(1);
but I can't seem to get both to be called. If I try:
stubbedThing.callsArg(0).callsArg(1);
or
stubbedThing.callsArg(0);
stubbedThing.callsArg(1);
then sinon will only ever call the second argument. If I wire it up in the other order, then sinon will call the first arg. However, I'd like both to be called one after the other.
If I have a stub for a function that takes 2 callbacks, how can I wire up sinon.js to call both callbacks when the stubbed function is invoked?
For example - here's function that I want to stub which takes 2 functions as arguments:
function stubThisThing(one, two) {
... one and two are functions ...
... contents stubbed by sinon.js ...
}
I can use sinon to call either one of the arguments:
stubbedThing.callsArg(0);
or
stubbedThing.callsArg(1);
but I can't seem to get both to be called. If I try:
stubbedThing.callsArg(0).callsArg(1);
or
stubbedThing.callsArg(0);
stubbedThing.callsArg(1);
then sinon will only ever call the second argument. If I wire it up in the other order, then sinon will call the first arg. However, I'd like both to be called one after the other.
Share Improve this question edited Apr 23, 2015 at 9:56 serg10 asked Apr 23, 2015 at 9:11 serg10serg10 32.7k16 gold badges75 silver badges94 bronze badges 4-
have you checked
calledWith
method – Pawan Commented Apr 27, 2015 at 5:40 -
@Pawan - Do you mean the
calledWith
function from the spy API? That's really not what I am looking for. I want to alter the behaviour of a stub. – serg10 Commented Apr 27, 2015 at 7:50 -
sinon.js only supports calling at most one callback per stub per call. It can call multiple callbacks on multiple calls eg.
stubbedThing.onCall(0).callsArg(0); stubbedThing.onCall(1).callsArg(1);
. Or you can manuallycallArg
after yourstubbedThing
is called: e.g.stubbedThing(firstArg, secondArg); stubbedThing.callArg(0); //calls firstArgs; stubbedThing.callArg(1) // calls secondArg
– nemesv Commented Apr 27, 2015 at 15:41 - @serg10: why dont you put this question on their github as an issue, may be they will e up with something.. – Pawan Commented Apr 28, 2015 at 4:37
2 Answers
Reset to default 7 +50This is not a classic scenario, since not many methods would call two methods sequentially, and I guess thats why it isn't supported. But, be calm, the solution is easy:
var subject = {
method: function(one, two) {}
};
var stub = sinon.stub(subject, 'method', function(one, two) {
one();
two();
});
subject.method(
function() { console.log('callback 1'); },
function() { console.log('callback 2'); });
Side note: This also gives the option for choosing if one or two should be called first.
Why don't you skip sinon altogether?
var obj = { stubMe: function(cb1, cb2) {} };
var originalMethod = obj.stubMe;
obj.stubMe = function(cv1, cb2) {
//Whatever logic you wish
cb1();
cb2();
};
//Do your test
obj.stubMe = originalMethod; //Restore
This way you can even continue to use sinon's APIs, if you wish:
var stub = sinon.stub();
obj.stubMe = function(cb1, cb2) {
stub.apply(stub, arguments);
//Rest of whatever logic you wanted here
};
obj.stubMe();
expect(stub.calledOnce()).to.be(true); //That would pass
What'd you think?
本文标签: javascriptsinonjs stubcan you call more than one callback on a single stubbed functionStack Overflow
版权声明:本文标题:javascript - sinon.js stub - can you call more than one callback on a single stubbed function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743994161a2572686.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论