我正在用Ember-qunit编写单元测试。我想在performance.now上设置一个自定义值。
我尝试过sinon.stub(performance,'now', 60000);,但这不起作用。我得到了TypeError: stub(obj, 'meth', fn) has been removed.
如何使用sinon.js来存根performance.now()?
谢谢
发布于 2020-03-21 04:56:26
不确定您的第三个参数(60000)应该是什么,因为我不熟悉performance.now(),但这不是对Sinon.stub() (there is no 3rd parameter)的有效调用。但是,根据文档,您应该能够捕获存根函数,然后对其调用一个方法来指示所需的返回值:
const stub = sinon.stub(performance, 'now');
stub.returns(60000);然后,当调用存根时,您应该得到:
console.log( stub() ); // 60000您可以在此jsfiddle example中看到此功能。
发布于 2020-06-19 21:01:56
创建全局对象,如下所示:
global.performance = {
now() {
return <returning_value>; // use Date.now() or any value
};现在您可以访问performance.now()了
https://stackoverflow.com/questions/60780014
复制相似问题