Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stub a method conditionally (on call # or arguments) #735

Closed
brian-mann opened this issue Apr 23, 2015 · 2 comments
Closed

Stub a method conditionally (on call # or arguments) #735

brian-mann opened this issue Apr 23, 2015 · 2 comments

Comments

@brian-mann
Copy link

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.

@cjohansen
Copy link
Contributor

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.

@maurermax
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants