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

Mock's behavior with connected once functions is surprising #984

Closed
yaronyg opened this issue Feb 11, 2016 · 2 comments
Closed

Mock's behavior with connected once functions is surprising #984

yaronyg opened this issue Feb 11, 2016 · 2 comments
Labels

Comments

@yaronyg
Copy link

yaronyg commented Feb 11, 2016

  var foo = { m: function () {}};
  var mock = sinon.mock(foo);
  mock.expects('m').once().withExactArgs(23).returns(23).once().withExactArgs(45).returns(45);
  console.log(foo.m(45));
  console.log(foo.m(23));
  mock.verify();

This test fails, but for the wrong reason. What seems to be happening is that if one strings together multiple once's in the same call then only the last one is remembered. So this test fails because I get an unexpected call on m(23).

Just to be complete I also tried:

  var foo = { m: function () {}};
  var mock = sinon.mock(foo);
  mock.expects('m').once().withExactArgs(23).returns(23).once().withExactArgs(45).returns(45);
  console.log(foo.m(23));
  console.log(foo.m(45));
  mock.verify();

And it also fails because the m(23) call was unexpected. Again confirming that the last once overwrites the first once.

Personally I found this very surprising. Is this the intended behavior?

@launchcg-ztonia
Copy link

Yea, withArgs are not chainable as they do not return a reference to the 'root' of the method, they return a branched path defined with the conditional. So for example you could do
mock.expects('m').once().withArgs(5).withArgs(7)
And I'm PRETTY sure, what you really get is something that needs both a 5 and a 7 as args. Spot check me on that.

But I am certain that do do what you want, gotta declare them separately. Don't know if there's conversations around that, but I've been working with that for some time.

@stale
Copy link

stale bot commented Jan 13, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jan 13, 2018
@stale stale bot closed this as completed Jan 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants