Sinon: Wrong value returned when configuring multiple withArgs using matchers

Created on 25 Nov 2016  路  5Comments  路  Source: sinonjs/sinon

  • 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
Awaiting Response Bug Help wanted Needs investigation Unverified

Most helpful comment

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.

All 5 comments

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

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

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.

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.

This seems to be fixed, I am closing it

Was this page helpful?
0 / 5 - 0 ratings

Related issues

OscarF picture OscarF  路  4Comments

optimatex picture optimatex  路  4Comments

ALeschinsky picture ALeschinsky  路  4Comments

kbirger picture kbirger  路  3Comments

NathanHazout picture NathanHazout  路  3Comments