You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I can't seem to find an easy way to do this in sinon...
I basically want to stub a method, but only conditionally.
For instance, I want the normal non-stubbed behaviors on the 1st through 3rd call. Meaning, I want to create my stub ahead of time, but just call through to the real method. But say on the 4th call, now I want to stub the method, and force its return.
Alternatively it would be even nicer to be able to partially match arguments and only apply the stub when it matches those arguments.
That way I wouldn't have to worry about the call order, but say "when the method in question is called with these arguments, return this value".
Currently I really can't even use sinon to do this. You can change a stubs return values conditionally, but once you stub it, it is STUBBED. There's no way to apply a filter to allow regular calls to go through. Right now I'm just monkey patching the method and manually inspecting the arguments to decide to change its behavior or call through. Would love a simpler solution.
The text was updated successfully, but these errors were encountered:
This sounds like an elaborate and overcomplicated test scenario. I find it highly unlikely that Sinon will support this kind of scenario. My advice is to review the design of your components and/or tests, maybe there's a simpler way to test your code than what you're currently attempting.
Okay this apparently very very old. I try to add a use case here anyways that seems reasonable to me for that kind of a situation. When using a request library (in my specific example it could be superagent). I might be requesting multiple responses within a function under test from different domains/URLs. It might be that one of these requests I need to provide a stubbed resposne for while for the others not.
const superagent = require('superagent');
async function funcToTest() {
const stubbedResponse = await superagent.get('http://domainineedtostubfor.com');
const realResponse = await superagent.get('https://iwanttoreachthisduringtest.com');
// do something with stubbedResponse and realResponse....
}
Writhing a test for this I would like to stub superagent.get but only for a give input parameter.
I can't seem to find an easy way to do this in sinon...
I basically want to stub a method, but only conditionally.
For instance, I want the normal non-stubbed behaviors on the 1st through 3rd call. Meaning, I want to create my stub ahead of time, but just call through to the real method. But say on the 4th call, now I want to stub the method, and force its return.
Alternatively it would be even nicer to be able to partially match arguments and only apply the stub when it matches those arguments.
That way I wouldn't have to worry about the call order, but say "when the method in question is called with these arguments, return this value".
Currently I really can't even use sinon to do this. You can change a stubs return values conditionally, but once you stub it, it is STUBBED. There's no way to apply a filter to allow regular calls to go through. Right now I'm just monkey patching the method and manually inspecting the arguments to decide to change its behavior or call through. Would love a simpler solution.
The text was updated successfully, but these errors were encountered: