/**
* "Enables" Sinon proxies (stubs, mocks) of Streamline.js proxy functions. Call this function
* after you have stubbed methods of obj. Otherwise original Streamline.js proxies are called
* regardless of Sinon proxying.
*
* Works by removing the 'fiberized' properties from the Sinon proxy functions. Sinon.restore()
* restores the original Streamline.js proxy with its fiberized properties.
*
* @param {Object} obj - object whose Sinon proxies to "enable"
*/
function enableSinonStubs(obj) {
_.forOwn(obj, function (value) {
if (value.isSinonProxy) {
_.forOwn(value, function (nestedValue, nestedKey) {
// Assuming that we're using the fibers generation option.
// The key is different with other options.
if (_.startsWith(nestedKey, 'fiberized')) {
delete value[nestedKey];
}
})
}
});
}
var foo = _streamline.async(function _$$foo$$(_2) {
var bar = _streamline.async(function _$$bar$$(_3) {
return 1;
}, 0, 1);
return bar["fiberized-0"](true);
}, 0, 1);
If you delete the "fiberized-0" property on bar, this code will break.
But this won't happen if you stub methods because method calls are resolved dynamically. So you should be on the safe side.
Bruno
The kind of situation you described probably doesn't happen in practice when unit testing. I could not mock the internal function bar() when testing foo() since I don't have access to it. If I needed to, I could move it to a separate component. The functions I need to mock are usually in objects loaded by require().
-Anssi