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

Sinon method chaining weird behaviour #739

Closed
phnah opened this issue Apr 24, 2015 · 2 comments
Closed

Sinon method chaining weird behaviour #739

phnah opened this issue Apr 24, 2015 · 2 comments

Comments

@phnah
Copy link

phnah commented Apr 24, 2015

Sinon.js (v1.14.1) has a weird behaviour in method chaining, not sure it's meant to be or a bug:

This (JSBin):

var myStub = sinon.stub().withArgs(1).returns("one").withArgs(2).returns("two");

Will always return "two" no matter what argument you call myStub with.

On the other hand, this (JSBin):

var myStub = sinon.stub();
myStub.withArgs(1).returns("one").withArgs(2).returns("two");

works fine. I'm not sure that SinonJS supports chaining or not.

Thanks, guys.

@mantoni
Copy link
Member

mantoni commented Apr 24, 2015

The "implementation detail" behind this is, that withArgs and returns returns a "behavior" and not the stub instance. The behaviors can be chained. In your first snippet, you assign a behavior instance to myStub.

I can see that this is not really intuitive, but it's not a bug. Maybe it could be clarified in the docs.

@mantoni mantoni closed this as completed Apr 24, 2015
@phnah
Copy link
Author

phnah commented Apr 24, 2015

Thanks a lot @mantoni.

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