您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用Sinon.js存根类方法

使用Sinon.js存根类方法

您的代码正在尝试在一个函数上存根Sensor,但是您已在上定义了该函数Sensor.prototype

sinon.stub(Sensor, "sample_pressure", function() {return 0})

基本上与此相同:

Sensor["sample_pressure"] = function() {return 0};

但是足够聪明地看到它Sensor["sample_pressure"]不存在。

因此,您想要做的是这样的事情:

// Stub the prototype's function so that there is a spy on any new instance
// of Sensor that is created. Kind of overkill.
sinon.stub(Sensor.prototype, "sample_pressure").returns(0);

var sensor = new Sensor();
console.log(sensor.sample_pressure());

要么

// Stub the function on a single instance of 'Sensor'.
var sensor = new Sensor();
sinon.stub(sensor, "sample_pressure").returns(0);

console.log(sensor.sample_pressure());

要么

// Create a whole fake instance of 'Sensor' with none of the class's logic.
var sensor = sinon.createStubInstance(Sensor);
console.log(sensor.sample_pressure());
其他 2022/1/1 18:14:03 有396人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶