【发布时间】:2019-05-25 14:17:07
【问题描述】:
假设我有一个结构如下的类:
// Some class that calls super.get() and adds an additional param
export default class ClassB extends ClassA {
private foo: string;
constructor(params) {
super(params);
this.foo = 'bar';
}
public async get(params?: { [key: string]: any }): Promise<any> {
return super.get({
foo: this.foo,
...params,
});
}
}
我想测试是否使用提供的参数以及附加的 { foo: 'bar' } 调用了 super.get()。
import ClassA from '../../src/ClassA';
import ClassB from '../../src/ClassB';
jest.mock('../../src/ClassA');
jest.unmock('../../src/ClassB');
describe('ClassB', () => {
describe('get', () => {
beforeAll(() => {
// I've tried mock implementation on classA here but didn't have much luck
// due to the extending not working as expected
});
it('should get with ClassA', async () => {
const classB = new ClassB();
const response = await classB.get({
bam: 'boozled',
});
// Check if classA fetch mock called with params?
});
});
});
如何检查 classA.fetch 是否实际上是使用我期望的参数调用的?
我做错了什么吗?
感谢您的帮助!
【问题讨论】:
标签: typescript mocking jestjs ts-jest