Wrong value returned when configuring multiple withArgs using matchers #1197

Open
martynsmall opened this Issue Nov 25, 2016 · 4 comments

Projects

None yet

3 participants

@martynsmall
  • Sinon version : 1.17.6
  • Environment : Browser

Running the function stubbed in the below example should return 1 as this satisfies the first withArgs setup. However, with the introduction of the second withArgs setup the return value becomes 2.

function Example() {
  this.doSomething = function () { };
}

var example = new Example();
sinon.stub(example, 'doSomething');

example.doSomething.withArgs(sinon.match(function() { return true; })).returns(1);
example.doSomething.withArgs(sinon.match(function() { return false; })).returns(2);

equal(example.doSomething({}), 2); //should fail.  passes
@fatso83
Contributor
fatso83 commented Jan 9, 2017

Hi, this does seem wrong. Mind seeing if this bug is present in Sinon 2 (npm i sinon@next)?

@martynsmall

Just tried it in Sinon 2 and the bug is still present.

@lucasfcosta
Contributor
lucasfcosta commented Jan 14, 2017 edited

Hi friends, I have just tested this against master and it seems to be working.

Here is the code I've written to test it:

describe("#1197", function () {
    it.only("Returns correct value according to matcher with multiple .withArgs", function () {
        function Example() {
          this.doSomething = function () { };
        }

        var example = new Example();
        sinon.stub(example, 'doSomething');

        example.doSomething.withArgs(sinon.match(function() { return true; })).returns(1);
        example.doSomething.withArgs(sinon.match(function() { return false; })).returns(2);

        // This passes, as it should
        assert.equals(example.doSomething({}), 1);
    });
});

As you can see, the stubbed function returns 1 as expected due to its matcher always returning true.

Let me know if I did anything wrong or if I misunderstood anything.

@martynsmall

Hi, I've just retested it myself on a build from master and it is indeed fixed now. The version I installed earlier is dated 31/12/2016 so this bug was obviously fixed very recently! Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment