【发布时间】:2019-08-30 17:50:58
【问题描述】:
我在 NestJS 6 上有一项服务。我使用 TypeORM 来处理 MySQL。
当我尝试使用 @Transaction() 和 @TransactionManager() 装饰器为方法编写单元测试时,我收到错误:ConnectionNotFoundError: Connection "default" was not found.。
@Put(':id')
@Transaction()
update(
@Body() someData: SomeDto,
@Param('id') id: number,
@UserDecorator() user: User,
@TransactionManager() manager: EntityManager,
) {
return this.someService.update(id, someData, user, manager);
}
我的测试:
beforeEach(async () => {
manager = new EntityManager(null);
someService = new SomeService();
someController = new SomeController(someService);
});
describe('update', () => {
it('should update', async () => {
jest.spyOn(someService, 'update').mockResolvedValue(result);
await expect(await someController.update(someData, 1, user, manager)).toBe(result);
expect(someService.update).toBeCalledWith(1, someDto, user, manager);
});
});
有没有更好的编写和测试方法?
【问题讨论】:
-
你能添加你的单元测试吗?
-
没什么特别的。我更新了我的问题。
-
像这样使用 typeorm 的
@Transaction违背了 Nest 的 DI,因此使测试变得非常困难,请参阅这个问题:github.com/nestjs/typeorm/issues/57 关于如何在 Nest 中进行事务的官方文档很快就会出现。
标签: javascript node.js unit-testing nestjs typeorm