sinon.spy 有 2 个参数,对象和函数名称。
我有一个模块如下所示:
module.exports = function xyz() { }
我如何定义间谍xyz
?我没有可以使用的对象名称。
想法?
如果你使用 ES6 模块导入功能,上面的方法实际上不起作用,如果你使用 ES6 模块导入功能,我发现你实际上可以像这样监视默认值。
// your file
export default function () {console.log('something here');}
// your test
import * as someFunction from './someFunction';
spyOn(someFunction, 'default')
如中所述http://2ality.com/2014/09/es6-modules-final.html http://2ality.com/2014/09/es6-modules-final.html#the-default-export-is-just-another-named-export
默认导出实际上只是一个具有特殊名称default的命名导出
因此, import * as someFunction 使您可以访问整个 module.exports 对象,从而允许您监视默认值。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)