substantial / sinon-stub-promise

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

how do I verify arguments passed to the stub ? #26

Open walshe opened 7 years ago

walshe commented 7 years ago

In code below I would like to check the params that I passed to the stubbed function..

const sinon = require('sinon'); var sinonStubPromise = require('sinon-stub-promise'); sinonStubPromise(sinon);

var serviceRequest = require("../../mysql/models")['ServiceRequest']; var providerRequest = require("../../mysql/models")['ProviderRequest'];

var serviceRequestCreateStub = sinon.stub(serviceRequest, 'create'); var providerRequestCreateStub = sinon.stub(providerRequest, 'create');

... ...

serviceRequestCreateStub.callsFake(function(obj){ / would like to verify that obj passed in was correct here/}).returnsPromise().resolves({},true);

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

I'm not exactly sure what you are trying to do. The documentation for callsFake seems like it just allows you to override the behavior of the stub when invoked.

In order to verify that your stub was called with the correct args, you could do myStub.withArgs(obj).returnsPromise().resolves({}, true).

Hope that helps!

walshe commented 7 years ago

thanks, I got it to work @astephenb btw I notice that when the args are not called properly that the error message is pretty bad and not very easy to diagnose

providerRequestCreateStub.withArgs({ vroomMessageUuid: messages[0].data.attributes.messageUuid, recipient: messages[0].data.attributes.to, timestamp: sinon.match.any, twilioMessageSid: "12345678" }).returnsPromise().resolves({},true);

is there anyway to tell it to give a more meaningful error message ? thanks