Simplified problem case:
export class MyClass {
constructor() {
this.myMethod();
}
myMethod() {
console.log(42);
}
}
Testing the constructor:
describe('CLASS: MyClass', () => {
let sut: MyClass;
beforeEach(() => {
jest.clearAllMocks();
sut = new MyClass();
});
describe('CONSTRUCTOR', () => {
test('should construct correctly and call myMethod', () => {
const spy = jest.spyOn(sut, 'myMethod').mockImplementationOnce(jest.fn());
expect(sut).toBeTruthy();
expect(spy).toHaveBeenCalled();
});
});
});
Of course this doesn't work, as the spy is initiated after sut is constructed, so it can't register the call.
Neither is it possible to initiate the spy before sut, as it can't spy on something that doesn't exist yet.
Nor did I have success trying to spy on MyClass.prototype.
Sure, I could spy on the implementation details of myMethod (basically jest.spyOn(console, 'log'). But that defies the separation of units for testing.
It's probably trivial, but what am I missing, how to get this very simple test to work?
1条答案
按热度按时间xqk2d5yq1#
您应该监视
MyClass
,而不是示例化的类sut
。你仍然可以在不嘲笑实现的情况下监视方法,但是在这里最好避免执行其中的代码并在控制台中记录42。