我已经为 Nest 应用程序构建了一个新的模块和服务,它具有循环依赖关系,当我运行应用程序时,它会成功解析,但是当我运行测试时,我的模拟模块(TestingModule)无法解析我的新服务的依赖关系创建的。
通过与“MathService”的循环依赖关系创建的“LimitsService”示例:
@Injectable()
export class LimitsService {
constructor(
private readonly listService: ListService,
@Inject(forwardRef(() => MathService))
private readonly mathService: MathService,
) {}
async verifyLimit(
user: User,
listId: string,
): Promise<void> {
...
this.mathService.doSomething()
}
async someOtherMethod(){...}
}
Run Code Online (Sandbox Code Playgroud)
MathService 在其方法之一中调用 LimitService.someOtherMethod。
这就是“MathService”测试模块的设置方式(没有“LimitsService”之前一切正常):
const limitsServiceMock = {
verifyLimit: jest.fn(),
someOtherMethod: jest.fn()
};
const listServiceMock = {
verifyLimit: jest.fn(),
someOtherMethod: jest.fn()
};
describe('Math Service', () => {
let mathService: MathService;
let limitsService: LimitsService;
let listService: ListService;
let httpService: HttpService;
beforeEach(async () => …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Joi 验证具有多个查询的 Nest.js 上的 GET 请求。我了解如何UsePipes在单个参数上使用和验证单个对象。但是我现在有一个具有多个查询的端点,这是我的控制器:
@Get(':cpId/search')
@UsePipes(new JoiValidationPipe(queryDTOSchema))
async getSomethingByFilters(
@Param('cpId') cpId: string,
@Query('startDate') startDate?: number,
@Query('endDate') endDate?: number,
@Query('el') el?: string,
@Query('fields') fields?: string,
@Query('keyword') keyword?: string,
@Query('page') page?: number,
@Query('limit') limit?: number,
)...Run Code Online (Sandbox Code Playgroud)
现在UsePipes正在针对每个查询验证相同的架构,但我不明白如何单独验证每个查询。
有没有办法分别验证每个查询?我找不到任何引用,我能想到的唯一解决方案是将所有这些查询转换为单个对象,在这种情况下这是不可取的。