假设我对数据库进行了事务更新
\n...\n\nconst sendMoney = prisma.account.update({ where: { id: 1 }, data: { ... } })\nconst receiveMoney = prisma.account.update({ where: { id: 2 }, data: { ... } })\n\nconst [accountOfSender, accountOfReceiver] = await prisma.$transaction([\n sendMoney,\n receiveMoney\n])\n\n...\nRun Code Online (Sandbox Code Playgroud)\n我有一个单元测试来验证这笔交易
\ndescribe(\'The money transfer use case\', () => {\n let mockedContext: MockContext\n let moneyTransferUseCase: MoneyTransferUseCase\n\n beforeEach(() => {\n mockedContext = createMockedContext()\n moneyTransferUseCase = new MoneyTransferUseCase(mockedContext.prisma)\n })\n\n it(\'should transfer money between two accounts\', () => {\n ...\n\n const updatedAccount = { ... }\n\n mockedContext.prisma.account.update.mockReturnedValue(Promise.resolve(updatedAccount))\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - throws an error\n\n ...\n })\nRun Code Online (Sandbox Code Playgroud)\n错误信息
\nTS2345: Argument of type \'Promise<Account>\' is not assignable to parameter of type \'Prisma__AccountClinet<Account>\'.\n Type \'Promise<Account>\' is missing the following properties from type \'Prisma__AccountClient<Account>\': _dmmf, _fetcher, _queryType, _rootField, and 11 more.\n\n 63 mockedContext.prisma.account.update.mockReturnValue(Promise.resolve(updatedAccount))\nRun Code Online (Sandbox Code Playgroud)\n这个错误消息中最重要的是这个
\nType \'Promise<Account>\' is missing the following properties from type \'Prisma__AccountClient<Account>\': _dmmf, _fetcher, _queryType, _rootField, and 11 more.\nRun Code Online (Sandbox Code Playgroud)\n我可以理解Promise.resolve(updatedAccount)不足以嘲笑棱镜承诺。但如何嘲笑它呢?文档对此没有任何说明。有任何想法吗?
更新
\n我想了一会儿,决定我可以嘲笑prisma.$transaction([ ... ])调用本身
TS2345: Argument of type \'Promise<Account>\' is not assignable to parameter of type \'Prisma__AccountClinet<Account>\'.\n Type \'Promise<Account>\' is missing the following properties from type \'Prisma__AccountClient<Account>\': _dmmf, _fetcher, _queryType, _rootField, and 11 more.\n\n 63 mockedContext.prisma.account.update.mockReturnValue(Promise.resolve(updatedAccount))\nRun Code Online (Sandbox Code Playgroud)\n它准确返回我需要的内容,但中间调用
\nType \'Promise<Account>\' is missing the following properties from type \'Prisma__AccountClient<Account>\': _dmmf, _fetcher, _queryType, _rootField, and 11 more.\nRun Code Online (Sandbox Code Playgroud)\nundefined测试期间返回。除了将这些结果传递给prisma.$transaction。
所以看起来测试按预期工作,但不确定这是否是一个好方法。
\n| 归档时间: |
|
| 查看次数: |
2142 次 |
| 最近记录: |