我用Jest在NodeJS上编写单元测试。有一个方法可以返回一个实体或实体数组。当我尝试模拟此方法的返回值时,我只能传递数组,但需要一个实体。
npm i jest typeorm
const manager = new EntityManager(null);
const sale = new Sale();
jest.spyOn(manager, 'create').mockReturnValue(sale);
最后一个字符串导致错误: Argument of type 'Sale' is not assignable to parameter of type '{}[]'. Type 'Sale' is missing the following properties from type '{}[]': length, pop, push, concat, and 26 more.
您可以使用 Args 来实现这一点,
与Args(...参数( →
指定用于调用具有指定参数的间谍
的策略例如:
spyOn(something, 'func').withArgs(arg1).returnValue(obj);
然后再次间谍与不同的返回值不同
spyOn(something, 'func').withArgs(arg2).returnValue(arrayValue);