substantial / sinon-stub-promise

Synchronous Promise stubbing for Sinon.JS
MIT License
85 stars 11 forks source link

Using withArgs? #9

Closed recipher closed 9 years ago

recipher commented 9 years ago

In your docs you mention chaining withArgs.

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

Any thoughts on whether that might be feasible?

a-b-r-o-w-n commented 9 years ago

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!

recipher commented 9 years ago

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

amay commented 9 years ago

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!

rhagigi commented 8 years ago

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');
  });
rhagigi commented 8 years ago

A simple change to allow usage we want is: https://github.com/rhagigi/sinon-stub-promise/compare/synchronousResolve...rhagigi:fixWIthArgs

a-b-r-o-w-n commented 8 years ago

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

rhagigi commented 8 years ago

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.