如何在 NestJS 的 Jest 中模拟具有关系的 Sequelize 模型?

问题描述 投票:0回答:1

我正在尝试为 NestJS 中的模块编写测试。我拥有的一个模型与另一个模型有关系。当我模拟它时,我遇到以下错误:

const mockInvoiceModel = {}

beforeEach(async () => {
  const module: TestingModule = await Test.createTestingModule({
    providers: [InvoiceService, { provide: getModelToken(InvoiceModel), useValue: mockInvoiceModel }],
  }).compile();

错误:

Cannot find module 'src/subscription/models/subscription.model' from 'invoice/models/invoice.model.ts

这就是发票模型的关系:

@ForeignKey(() => SubscriptionModel)
@Column({type: DataType.UUID })
subscriptionId: UUID;

@BelongsTo(() => SubscriptionModel)
subscription: SubscriptionModel;

@HasMany(() => TransactionModel)
payments: TransactionModel[];

发票模型与订阅有关系。我如何模拟这个模型?

我期望通过在测试模块中使用 getModelToken(InvoiceModel) 为 InvoiceModel 提供模拟值

unit-testing jestjs nestjs backend
1个回答
0
投票

通过将文件导入从

src/subscription/models/subscription.model
更改为
../subscription/models/subscription.model
解决了该问题。

© www.soinside.com 2019 - 2024. All rights reserved.