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

Combination of callsArgWith and returns methods #1268

Closed
mousavian opened this issue Feb 15, 2017 · 2 comments
Closed

Combination of callsArgWith and returns methods #1268

mousavian opened this issue Feb 15, 2017 · 2 comments

Comments

@mousavian
Copy link

mousavian commented Feb 15, 2017

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:

//actual code:
obj = {
  method: (arg1, arg2) => {
     //do stuff
     return arg1+arg2
  }
}
obj.method(1, 2)

// test:
sinon.stub(obj, 'method')
  .processArgsAndReturn((arg1, arg2) => {
    return arg1*arg2
  })

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 = [ ... ]
          return callback(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

@lucasfcosta
Copy link
Member

Hi, @mousavian, how are you?

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:

var stub = sinon.stub(object, "method", function (arg1, arg2) {
  return arg1 * arg2;
});

// Stubs have Spy properties
// You can also check what this stub returned on each one of these calls
stub.callCount // Returns how many times stub was called
stub.calledWith('example', 'foo') // Checks if stub was called with these args
stub.returned('bar') // Checks if stub returned 'bar' at least once
stub.alwaysReturned('bar') // Checks if stub returned 'bar' every time it was called

You can see more things you can check at: http://sinonjs.org/docs/#spies-api

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.

@mousavian
Copy link
Author

Hello @lucasfcosta , I'm great, how are things with you? :)

Yes, that is what i was looking for and I am wondering how I could miss that in the top of the documentation.

Thanks for your your reply and information about future changes. Cheers 👍

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

2 participants