Reputation: 3957
I'm trying to test an EmberJS app that uses Ember.run.debounce
. I'd like to use Sinon to stub Ember.run.debounce
so that it will just call the debounced method synchronously. Something like this:
debounceStub = sinon.stub(Ember.run, 'debounce')
debounceStub.callsArgWith(1, debounceStub.args[0][2])
to make this code run synchronously:
Ember.run.debounce(@, @handleData, list, 500)
but handleData()
is being called with an undefined argument rather than list
. Any help figuring out how to pass list
in the callsArgWith
call would be greatly appreciated.
Thank!
Upvotes: 0
Views: 369
Reputation: 926
The reason handleData
is called with undefined is because the point at which you're defining the behaviour of the stub (callsWithArg(...)
) is before the stub has been invoked (through executing your unit-under-test), so the reference to the args is not available yet.
It's a bit ugly, but one solution is to manually invoke the method passed to debounce
, something like...
debounceStub = sinon.stub(Ember.run, 'debounce')
//...execute unit-under-test so that `debounce` is called...
//Then pull out the arguments from the call
var callArgs = debounceStub.firstCall.args.slice();
var targetObject = callArgs.shift();
var methodToCall = callArgs.shift();
var methodArgsToPass = callArgs.shift();
//Manually invoke the method passed to `debounce`.
methodToCall.apply(targetObject, methodArgsToPass);
//perform your assertions...
Upvotes: 1