Developing a callback-driven API, I would like to express that a certain function as to be called with a specific set of parameters and “any” function (the callback).
Jasmine can do the following:
var serviceFunction = jasmine.createSpy();
var functionUnderTest = create(serviceFunction);
var thing = 'arbitrary/thing'
functionUnderTest(thing);
expect(serviceFunction).toHaveBeenCalledWith(thing, jasmine.any(Function));
Have sinon/buster.js similar capabilities? So far I’m testing only the first argument, but I’d really like to express the need for a callback in the test.
This is what I have so far:
var serviceFunction = this.spy(); // or `sinon.spy()`
var functionUnderTest = create(serviceFunction);
var thing = 'arbitrary/thing'
functionUnderTest(thing);
assert.calledWith(serviceFunction, thing);
You should check out the sinon.match api (http://sinonjs.org/docs/#sinon-match-api)
Using sinon.match.func your example above would become:
var serviceFunction = this.spy(); // or `sinon.spy()`
var functionUnderTest = create(serviceFunction);
var thing = 'arbitrary/thing'
functionUnderTest(thing);
assert.calledWith(thing, sinon.match.func);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With