Question

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);
Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top