使用Sinon Stubbing获取方法

Stubbing a get method using Sinon

本文关键字:方法 获取 Stubbing Sinon 使用      更新时间:2023-12-30

我正在尝试用属性截断对象的get方法

工作良好:

sinon.stub(input.model, 'get');
input.model.get.returns(10);

但是考虑一下我们是否需要在对象中截取一些特定的属性

例如:

input.model.get('yourValue') 

↪这怎么能被掐断?知道吗?

stub.withArgs()应该可以随心所欲。看见http://sinonjs.org/docs/#stubs.

sinon.stub(input.model, 'get').withArgs('yourValue').returns(10);

Sinon已经改变了这个语法:

class Foo {
  get bar() { 
    return 'yolo'; 
  }
}
const myObj = new Foo();
sinon.stub(myObj, 'bar').get(() => 'swaggins');
myObj.bar; // 'swaggins'