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 mock expectations do not remember multiple withArgs() or withExactArgs() calls. #992

Closed
TonyApuzzo opened this issue Feb 24, 2016 · 7 comments

Comments

@TonyApuzzo
Copy link

SInon 1.17.3 doesn't seem to work with mocks as expected. I would think that I can treat mocks like stubs, but the with*Args() methods seem to only remember the last invocation, it doesn't create an internal map of argument to return value like you get with stubs.

describe('Sinon Verification', function () {
  it('Should work with mocks', function () {
    var u = { inc : function(x){} };
    var m = sinon.mock(u);
    var e = m.expects("inc");
    e.withExactArgs(0).returns(1);
    e.withExactArgs(1).returns(2);
    e.withExactArgs(2).returns(3);

    u.inc(2).must.eql(3); // OK
    u.inc(1).must.eql(2); // Fails expectation
    u.inc(0).must.eql(1); // Fails expectation
  });
});
@TonyApuzzo TonyApuzzo changed the title Sinon mock expectations do not allow multiple withArgs() or withExactArgs() Sinon mock expectations do not remember multiple withArgs() or withExactArgs() calls. Feb 24, 2016
@yaronyg
Copy link

yaronyg commented Jun 22, 2016

Sounds like #984

@hrimhari
Copy link
Contributor

@yaronyg not exactly. There were discussions about multiple withArgs() being chainable or not like #984 is trying to do. But the problem reported here is that mocks just can't have multiple withArgs(), regardless of their declaration being in the same chain or not.

@mroderick
Copy link
Member

It is correct that mocks only keep the last value of arguments to withArgs.

This has been the case since 84dbb22, which was committed on Apr 18, 2010.

git log -S "this.expectedArguments = slice.call(arguments);"

The same is true for withExactArgs, which calls withArgs.

I have created #1432 to document this issue

@hrimhari
Copy link
Contributor

@mroderick how would you test a loop over different objects if your mock can only accept the same parameters?

@mroderick
Copy link
Member

@mroderick how would you test a loop over different objects if your mock can only accept the same parameters?

With sinon.mock, you would have to create a new one for each member in your collection. Using sinon.stub, you can inspect each call.

@mroderick
Copy link
Member

Issue #1193 also has a good explanation of this deficiency.

@mroderick
Copy link
Member

Fixed by #1432

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

4 participants