substantial/sinon-stub-promise

Using withArgs?

Closed this issue · 7 comments

In your docs you mention chaining withArgs.

sinon.stub().withArgs(42).resolves('value')

Any thoughts on whether that might be feasible?

I think this should be possible. The current feature set has met most of our needs so there hasn't been any pressure to explore this more. We would welcome a PR though!

Marvellous. I'll see if I can find some time to investigate.

amay commented

Yes, this should definitely be feasible and would be a great addition. I took a quick look at how withArgs currently works with sinon's stubs and it was not immediately apparent to me how to move forward. If you can make some progress, that would be great!

All withArgs does is shallowly clone the stub and bring over anything defined as a "behavior". Calling withArgs after setting the return value (with returnsPromise) is just not using withArgs in the right spot, since on this library, .resolves() only mutates the state of the thenable, it does not change the thenable on the stub itself. That is doable though, and wouldn't require a ton of work.

The way to do this using this library as it is written is to do:

  it('works with withArgs()', function(){
    var myStub = sinon.stub();
    myStub.withArgs(42).returnsPromise().resolves('firstValue');
    myStub.withArgs(1).returnsPromise().resolves('anotherValue');

    myStub(42).then(function(arg) {
      resolveValue = arg;
    });
    expect(resolveValue).to.equal('firstValue');

    myStub(1).then(function(arg) {
      resolveValue = arg;
    });
    expect(resolveValue).to.equal('anotherValue');
  });

Would you mind creating a PR with that changeset? I will look at merging your other one this afternoon.

Sure thing, @astephenb, but I implemented it over-top of my other changeset. I can either pull it out (not hard) or just open the PR after the other one is merged.