2016-07-22 4 views
0

Sinon으로 인스턴스화 할 때 생성자가 메서드를 호출하도록하고 싶습니다. 그러나이 함수가 작동하지 않는 것 같습니다. 올바른 인스턴스 :Sinon 메서드를 호출하는 테스트 생성자

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

    someFunction() { 
    return 1; 
    } 
} 

... 시험

describe('constructor',() => { 

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

}); 

답변

3

봅니다 생성자를 호출하기 전에 Test.prototype.someFunction으로 감시한다. 이와 비슷한 것

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

그건 의미가 있습니다 ... 나는 그것에 대해 생각해 봤어야합니다. – Detuned

관련 문제