简体   繁体   English

测试构造函数使用Sinon调用方法

[英]Test constructor calling a method with Sinon

I'd like to ensure that the constructor is calling a method when instantiated with Sinon, however, I can't seem to get this to work, as I believe the sinon is not watching the correct instantiation: 我想确保构造函数在用Sinon实例化时正在调用一个方法,但是,我似乎无法使它正常工作,因为我相信sinon不会监视正确的实例化:

class Test {
  constructor() {
    this.someFunction();
  }

  someFunction() {
    return 1;
  }
}

... and the test ...和测试

describe('constructor', () => {

  it('should call someFunction()', () => {
    const spyFunc = new Spy(new Test(), 'someFunction');
    expect(spyFunc.calledOnce).to.be.true;
  });

});

Try to spy to Test.prototype.someFunction before invoking constructor. 在调用构造函数之前,请尝试监视Test.prototype.someFunction Something like this 像这样

sinon.spy(Test.prototype, 'someFunction')
const spyFunc = new Test();
expect(spyFunc.someFunction.calledOnce).to.be.true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM