最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Stub es6 getter setter with sinon - Stack Overflow

programmeradmin6浏览0评论

If you use the following es6 syntax for getters/setter

class Person {
  constructor(name) {
    this._name = name;
  }

  get name() {
    return this._name.toUpperCase();
  }

  set name(newName) {
    this._name = newName;
  } 
}

How would you stub the getter method ?

const john = new Person('john')
sinon.createSandbox().stub(john, 'name').returns('whatever')

Does not seem to be working.

If you use the following es6 syntax for getters/setter

class Person {
  constructor(name) {
    this._name = name;
  }

  get name() {
    return this._name.toUpperCase();
  }

  set name(newName) {
    this._name = newName;
  } 
}

How would you stub the getter method ?

const john = new Person('john')
sinon.createSandbox().stub(john, 'name').returns('whatever')

Does not seem to be working.

Share Improve this question asked Nov 21, 2019 at 12:41 Ronan QuillevereRonan Quillevere 3,8891 gold badge32 silver badges45 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

Github issue led me to : sinon js doc

stub.get(getterFn)

Replaces a new getter for this stub.

var myObj = {
    prop: 'foo'
};

sinon.stub(myObj, 'prop').get(function getterFn() {
    return 'bar';
});

myObj.prop; // 'bar'

stub.set(setterFn)

Defines a new setter for this stub.

var myObj = {
    example: 'oldValue',
    prop: 'foo'
};

sinon.stub(myObj, 'prop').set(function setterFn(val) {
    myObj.example = val;
});

myObj.prop = 'baz';

myObj.example; // 'baz'
发布评论

评论列表(0)

  1. 暂无评论