使用 Sinon.js 存根 performance.now()

我正在用 Ember-qunit 编写单元测试。我想在 performance.now 上设置自定义值。

我试过sinon.stub(performance,'now', 60000);了,但这没有用。我明白了TypeError: stub(obj, 'meth', fn) has been removed.

我如何使用 sinon.js 存根 performance.now()?

谢谢


慕田峪4524236
浏览 116回答 2
2回答

翻翻过去那场雪

创建一个全局对象,如:global.performance = {&nbsp; &nbsp;now() {&nbsp; &nbsp; return <returning_value>; // use Date.now() or any value};现在您可以访问performance.now()

守候你守候我

不确定您的第三个参数(60000)应该是什么,因为我不熟悉,但这不是对(没有第三个参数performance.now())的有效调用。但是,根据文档,您应该能够捕获存根函数,然后在其上调用方法以指示所需的返回值:Sinon.stub()const&nbsp;stub&nbsp;=&nbsp;sinon.stub(performance,&nbsp;'now'); stub.returns(60000);然后,当调用存根时,您应该得到:console.log(&nbsp;stub()&nbsp;);&nbsp;&nbsp;//&nbsp;60000您可以在这个jsfiddle 示例中看到此功能。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript