gpt4 book ai didi

javascript - 如何使用 JEST 模拟方法内的方法?

转载 作者:行者123 更新时间:2023-12-02 22:21:38 24 4
gpt4 key购买 nike

我正在尝试对名为 search 的方法进行单元测试它有三个方法,例如 buildSearchParameter , isUnknownFields , readAll 。我需要 mock 他们,但我不知道该怎么做。谁能帮我解决这个问题吗?

最佳答案

为了简化问题,我删除了它的静态类型。您可以使用 jest.spyOn 为这三个方法创建 stub 。这是单元测试解决方案:

index.ts:

import ModelUtility from './ModelUtility';

enum EndpointType {
SEARCH = 'SEARCH',
}

export class SomeClass {
schemaConstants;
errorConstants = {
invalidQueryParameters: 'invalid',
};

search = async (query, endpointConstants) => {
const searchParam = ModelUtility.buildSearchParameter(query, endpointConstants, this.schemaConstants);
// check for unknown fields
const isUnknownFields = ModelUtility.isUnknownFields(EndpointType.SEARCH, searchParam, this.schemaConstants);
if (isUnknownFields) {
return this.errorConstants.invalidQueryParameters;
}
return this.readAll(query, searchParam, endpointConstants);
};

readAll(...args) {
return 'real data';
}
}

ModelUtility.ts:

export default {
buildSearchParameter(...args) {
return 'real search parameter';
},
isUnknownFields(...args) {
return true;
},
};

index.spec.ts:

import { SomeClass } from './';
import ModelUtility from './ModelUtility';

describe('SomeClass', () => {
afterEach(() => {
jest.restoreAllMocks();
});
it('should return invalid query parameters', async () => {
const buildSearchParameterSpy = jest
.spyOn(ModelUtility, 'buildSearchParameter')
.mockReturnValueOnce('mocked search parameter');
const isUnknownFieldsSpy = jest.spyOn(ModelUtility, 'isUnknownFields').mockReturnValueOnce(true);

const instance = new SomeClass();
const actual = await instance.search('mocked query', 'endpointConstants');
expect(actual).toBe('invalid');
expect(buildSearchParameterSpy).toBeCalledWith('mocked query', 'endpointConstants', undefined);
expect(isUnknownFieldsSpy).toBeCalledWith('SEARCH', 'mocked search parameter', undefined);
});

it('should return correctly', async () => {
const buildSearchParameterSpy = jest
.spyOn(ModelUtility, 'buildSearchParameter')
.mockReturnValueOnce('mocked search parameter');
const isUnknownFieldsSpy = jest.spyOn(ModelUtility, 'isUnknownFields').mockReturnValueOnce(false);

const instance = new SomeClass();
jest.spyOn(instance, 'readAll').mockReturnValueOnce('mocked data');
const actual = await instance.search('mocked query', 'endpointConstants');
expect(actual).toBe('mocked data');
expect(buildSearchParameterSpy).toBeCalledWith('mocked query', 'endpointConstants', undefined);
expect(isUnknownFieldsSpy).toBeCalledWith('SEARCH', 'mocked search parameter', undefined);
});
});

带有覆盖率报告的单元测试结果:

 PASS  src/stackoverflow/59207566/index.spec.ts
SomeClass
✓ should return invalid query parameters (7ms)
✓ should return correctly (1ms)

-----------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
-----------------|----------|----------|----------|----------|-------------------|
All files | 72.73 | 100 | 57.14 | 73.68 | |
ModelUtility.ts | 20 | 100 | 0 | 20 | 2,3,5,6 |
index.ts | 88.24 | 100 | 80 | 92.86 | 24 |
-----------------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 4.891s, estimated 11s

关于javascript - 如何使用 JEST 模拟方法内的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59207566/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com