【发布时间】:2020-08-06 10:03:50
【问题描述】:
我在A.js 文件中有以下类,
class A {
constructor(name) {
this.name = name;
}
getResult() {
return this.name;
}
}
module.exports = A
在另一个名为controller.js的文件中,我是这样使用的,
const A = require('./A');
module.exports = {
doProcess: () => {
const a = new A('John');
console.log(a.getResult());
}
}
所以我的要求是,我想在为controller.js 编写单元测试时对类 A 及其方法进行存根。如何使用 sinon 实现这一点?
类似的,
const getResultStub = sinon.stub();
getResultStub().returns('success');
【问题讨论】:
-
Factory模式呢?您可以在控制器和以后的模拟中使用它。
标签: javascript unit-testing sinon