【发布时间】:2020-08-02 13:08:12
【问题描述】:
我需要在打字稿中监视多个方法。如何在 typescript 中实现?
// classA.ts
export class ClassA {
public methodA() {
this.methodB();
this.methodC();
return "ClassA";
}
public methodB() {}
public methodC() {}
}
// classATest.ts
import {ClassA} from './classA';
it('Sample Test', async () => {
const spyOn1 = jest.spyOn(ClassA, 'methodB');
spyOn1.mockImplementation(() => {return () => {}});
const spyOn2 = jest.spyOn(ClassA, 'methodC');
spyOn2.mockImplementation(() => {return () => {}});
const classA = new ClassA();
expect(classA.methodA()).toEqual('ClassA');
});
我收到错误声明 - Argument of type '"methodC"' is not assignable to parameter of type '"methodB" | "prototype"'.
我们不能在一个类上使用 spyOn 多个方法吗?还有其他方法可以实现吗?
【问题讨论】:
-
您需要在
ClassA.prototype或classA上模拟方法。
标签: javascript typescript unit-testing jestjs