带有 sinon 的 Stub es6 getter setter

如果您对 getter/setter 使用以下 es6 语法


class Person {

  constructor(name) {

    this._name = name;

  }


  get name() {

    return this._name.toUpperCase();

  }


  set name(newName) {

    this._name = newName;

  } 

}

你将如何存根 getter 方法?


const john = new Person('john')

sinon.createSandbox().stub(john, 'name').returns('whatever')

似乎没有工作。


GCT1015
浏览 164回答 1
1回答

慕桂英3389331

Github问题导致我:sinon js docstub.get(getterFn)替换此存根的新吸气剂。var myObj = {    prop: 'foo'};sinon.stub(myObj, 'prop').get(function getterFn() {    return 'bar';});myObj.prop; // 'bar'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'
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript