Ron*_*ere 4 javascript sinon ecmascript-6
如果您对 getter/setter 使用以下 es6 语法
class Person {
constructor(name) {
this._name = name;
}
get name() {
return this._name.toUpperCase();
}
set name(newName) {
this._name = newName;
}
}
Run Code Online (Sandbox Code Playgroud)
您将如何存根 getter 方法?
const john = new Person('john')
sinon.createSandbox().stub(john, 'name').returns('whatever')
Run Code Online (Sandbox Code Playgroud)
似乎不起作用。
stub.get(getterFn)
替换此存根的新吸气剂。
var myObj = {
prop: 'foo'
};
sinon.stub(myObj, 'prop').get(function getterFn() {
return 'bar';
});
myObj.prop; // 'bar'
Run Code Online (Sandbox Code Playgroud)
stub.set(setterFn)
为此存根定义一个新的设置器。
var myObj = {
example: 'oldValue',
prop: 'foo'
};
sinon.stub(myObj, 'prop').set(function setterFn(val) {
myObj.example = val;
});
myObj.prop = 'baz';
myObj.example; // 'baz'
Run Code Online (Sandbox Code Playgroud)