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
As I understand, .returns method, is allocating the value that needs to be returned in the time of stubbing (link).
But is there any way to decide what to return whenever the method we are stubbing is actually being invoked from the source code; based on arguments its receiving?
Please note that in my specific case, I don't know the exact value of parameters(arg1, arg2) when I am writing tests so I can't use .withArgs method, but the returned value depends on them.
A real world example?
It's a bit strange but I want to stub a method which invoke a callback and returns its response:
sinon.stub(dbConn,'doStuff').processArgsAndReturn((configObject,callback)=>{// do some stuff here using configObject// rows = [ ... ]returncallback(rows)})
I have also tried callArgWith, which is calling my callback, but there is no way to get the returned value from callback and returns it back for the stubbed method.
--
ps. I have actually implemented this locally which works fine but wanted to discuss here anyway that if there's better way to achieve this or maybe if it make sense, i'll send a PR.
Thanks
The text was updated successfully, but these errors were encountered:
Well, I think that you can just pass a simple function to the stub method. By doing this your function will replace the original one and you will have all the arguments passed to it available to do calculations and return them as you please.
For example:
varstub=sinon.stub(object,"method",function(arg1,arg2){returnarg1*arg2;});// Stubs have Spy properties// You can also check what this stub returned on each one of these callsstub.callCount// Returns how many times stub was calledstub.calledWith('example','foo')// Checks if stub was called with these argsstub.returned('bar')// Checks if stub returned 'bar' at least oncestub.alwaysReturned('bar')// Checks if stub returned 'bar' every time it was called
Also, please notice there are some major changes happening with the stub API (as you can see at #1239), and I've also just noticed that what we're talking about here can change in future versions, so I made this comment.
Let me know if there's anything else I can help you with or if you have any further doubts.
As I understand,
.returns
method, is allocating the value that needs to be returned in the time of stubbing (link).But is there any way to decide what to return whenever the method we are stubbing is actually being invoked from the source code; based on arguments its receiving?
For example:
Please note that in my specific case, I don't know the exact value of parameters(arg1, arg2) when I am writing tests so I can't use
.withArgs
method, but the returned value depends on them.A real world example?
It's a bit strange but I want to stub a method which invoke a callback and returns its response:
I have also tried
callArgWith
, which is calling my callback, but there is no way to get the returned value from callback and returns it back for the stubbed method.--
ps. I have actually implemented this locally which works fine but wanted to discuss here anyway that if there's better way to achieve this or maybe if it make sense, i'll send a PR.
Thanks
The text was updated successfully, but these errors were encountered: