Reputation: 824
I just want to know if it is possible to fake a callback on a stub argument.
This is basically what I want to achieve and I found nothing on Sinon's documentation:
function A(arg1, arg2, next){
return [arg1, arg2, next];
};
function B(string){
return string;
};
function C(){
return 'Mocked next';
};
var obj = {
A: A,
test: 'test'
};
var result1 = obj.A(1, 2, B('Next')); // result1 = [1, 2, 'Next']
sandbox.stub(obj, 'A')//.Argument[2].Returns(C());
var result2 = obj.A(1, 2, B('Next')); // result2 = [1, 2, 'Mocked next']
Is it possible?
Upvotes: 2
Views: 578
Reputation: 45810
Yes, it is possible.
sinon
doesn't provide a way to directly mock an argument of a stub
, but it does provide callsFake
which lets you create your own implementation.
You can create a stub
that calls the original implementation with the result of C()
passed as the third argument like this:
const original = obj.A; // capture original obj.A
sandbox.stub(obj, 'A').callsFake((...args) => original(args[0], args[1], C()));
const result = obj.A(1, 2, B('Next'));
sinon.assert.match(result, [1, 2, 'Mocked next']); // SUCCESS
Upvotes: 2