2
votes

I have a function like this:

var updateSomething = function(arg1, arg2, callback) {
  ...
  db.update(arg, function(err, res) {
    callback(err, res);
  });
};

I'm testing an API doing some requests that internally call to this function and I'm interested in known the callback value when it's called, How can I spy with Sinon the value of res and err in my unit test?

1
That's where dependency injection comes in handy. There's actually a nice getting started video: youtube.com/… - eol
Good information, thanks mate! - Borja Tur

1 Answers

0
votes

Assuming your API takes callback argument from outside:

var callbackSpy = sinon.spy();

yourAPI.fetchSomething(arg1, arg2, callbackSpy);

console.log(callbackSpy.args[0]);