【发布时间】:2021-01-21 02:07:08
【问题描述】:
我的项目有很多实用功能可以执行一些数据转换等。 这些函数不在类中,并且在许多实用程序文件中重复使用。
我对角度测试很陌生。搜索了很长一段时间,我找不到一种方法来存根测试文件的导入。示例如下:
/utils/helpers.ts
export const sumNumber = (a: number, b: number): number => {
return a + b;
}
/utils/file-i-want-to-test.ts
import { sumNumber } from "./helpers";
export const sumArrays = (arr1: number[], arr2: number[]): number[] => {
const len = arr1.length > arr2.length ? arr1.length : arr2.length;
const result = new Array(len).fill(0);
return result.map((_, index) => sumNumber(arr1[index] || 0, arr2[index] || 0));
}
/utils/file-i-want-to-test.spec.ts
import { sumArrays } from './file-i-want-to-test';
describe('Utilities', () => {
describe('Function - sumArrays', () => {
it('should return empty array', () => {
const actual = sumArrays([], []);
expect(actual).toEqual([]);
});
it('should call sumValue 2 times', () => {
const actual = sumArrays([1, 2], [3, 4]);
// How to test that sumArray is actually calling his dependency sumNumber?
});
});
});
问题
为了正确地对函数sumArrays进行单元测试,我需要存根它的依赖sumNumber。 怎么做?
【问题讨论】:
标签: angular typescript unit-testing stubbing angular-unit-test