I am testing a small function using AVA and Sinon. Function looks essentially like this (edited for brevity):
mergeDefaults: function (opts) {
**console.log('log 1 ->', opts);**
opts = _.defaultsDeep(opts, defaultOptions);
return opts;
}
I have written a test to ensure that the correct parameters are passed into _.defaultsDeep.
test.before(t => {
sandbox = sinon.sandbox.create();
defaultsDeepSpy = sandbox.spy(_, 'defaultsDeep');
mergeDefaults(Object.assign({}, testOptions));
});
test('mergeDefaults runs _.defaultsDeep with the correct parameters', t => {
**console.log('log 2 ->', defaultsDeepSpy.getCall(0).args[0]);**
t.is(defaultsDeepSpy.getCall(0).args[0], testOptions);
t.is(defaultsDeepSpy.getCall(0).args[1], defaultOptions);
});
test.after.always(t => {
sandbox.restore();
});
The problem I am running into is that log 1 and log 2 are not the same. As I understand it, the
spy.getCall(n).args[m]
returns the mth argument passed to the spied on function from the nth call to that function. However, in this case log 2 is actually returning what results from the function, not the first argument.
The options objects look something like this:
testOptions = {
key1: 11,
key2: 22,
key3: 33
}
defaultOptions = {
key1: 1,
key2: 2,
key3: 3,
key4: 4,
key5: 5
}
But the console.logs look like this:
log 1 -> testOptions = {
key1: 11,
key2: 22,
key3: 33
}
log 2 -> testOptions = {
key1: 11,
key2: 22,
key3: 33,
key4: 4,
key5: 5
}
So the spy.getCall(0).args[0] a.k.a "log 2" is actually returning what the "opts" becomes after the _.defaultsDeep runs. This feels like a bug in the Sinon library, but more likely I am doing something wrong. Help is much appreciated, thanks in advanced!
Aucun commentaire:
Enregistrer un commentaire