I'd like to test that a certain function gets called in a certain way, in my server-side javascript. I'm using Sinon mocks and stubs. Sinon has withArgs() method, to check that a function got called with certain arguments. Is it possible to use that method, withArgs(), if I pass a large complex callback function as one of the arguments?
var foo = function(){ method: function () {}};
// use: foo.method(function(){ console.log('something') } );
var spy = sinon.spy(foo, 'method');
spy.withArgs( ??? );
Your example is a little confusing, because you've defined foo
as a function, but the comment that follows calls foo.method()
:
var foo = function(){ method: function () {}};
// use: foo.method(function(){ console.log('something') } );
In any case, a "large complex callback function" is just an object. withArgs
returns a spy object that is filtered by the given args, and you can use a function as part of that filter. For example:
var arg1 = function () { /* large, complex function here :) */ };
var arg2 = function() {};
var foo = {
method: function () {}
};
var spy = sinon.spy(foo, 'method');
foo.method(arg1);
foo.method(arg2);
console.assert(spy.calledTwice); // passes
console.assert(spy.withArgs(arg1).calledOnce); // passes
console.assert(spy.withArgs(arg1).calledWith(arg1)); // passes
console.assert(spy.withArgs(arg1).calledWith(arg2)); // fails, as expected
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.