为什么supertest /笑话总是调用相同的端点显然当我插入一条不同的道路在我测试?我需要设置一些



这是我的控制器类:

@Crud({
model: {
type: InferenceFile,
},
query: {
join: {
model: {
eager: true,
},
file: {
eager: true,
},
},
},
params: {
id: {
type: 'uuid',
primary: true,
field: 'id',
},
},
})
@Controller('inference-files')
export class InferenceFilesController {
constructor(private service: InferenceFilesService) {
}
@Get('list/:algorithmId')
async getAllInferenceFiles(
@Param('algorithmId') algorithmId: number,
@Query('name') name: string,
@Query('page') page: number = 0,
@Query('size') limit: number = 1000,
) {
return await this.service.findAllByAlgorithmId(algorithmId, name, {
page,
limit,
});
}
@Get(':id')
async get(@Param('id') id: string) {
const result = await this.service.getFile(id);
result.presignedUrl = await this.service.getPresignedDownloadUrl(result);
result.presignedWordsUrl = await this.service.getPresignedWordsUrl(result);
return result;
}
@Get('status')
async getStatus(@Query('modelId') modelId: number) {
return await this.service.getStatus(modelId);
}
}

这是我的测试类出错的部分:

it('should return an array of inference file statuses', async () => {
// Act
let modelIdToPass = setUp.model1.id;
const { body } = await supertest
.agent(setUp.app.getHttpServer())
.get(`/inference-files/status?${modelIdToPass}`)
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200);
// Assert
expect(body).toEqual({
'test-id-2': 'FINISHED',
});
});
it('should return the inference file for the given id', async () => {
// Act
const { body } = await supertest
.agent(setUp.app.getHttpServer())
.get('/inference-files/' + TestConstants.INFERENCEFILE_ID_1)
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200);
// Assert
expect(body).toEqual({
'test-id-1': 'FINISHED',
});
});
it('should return the inference file for the given algorithm id', async () => {
// Act
const { body } = await supertest
.agent(setUp.app.getHttpServer())
.get('/inference-files/list/' + 9)
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200);
// Assert
expect(body).toBeDefined();
});

所有这些测试调用相同的端点:@Get(':id')但是当我注释掉其中的两个时,它成功了……或者更好的是,它调用剩下的"Get"方法。我找了一整天的答案,但似乎没有人遇到过同样的问题。

当我查看在线文档时,它没有提到任何关于此路由的内容。我也不想模拟实现(我已经看到了使用express路由的示例)。但那不是我想要的。我希望测试整个流程,因此它需要调用(全部或大部分)我的方法。我使用一个单独的数据库进行测试,所以不需要mock。

将控制器中@Get('status')的端点移动到@Get(':id')的端点上方。

因为:id先出现,它假设status实际上是一个id值,因此调用了错误的处理程序。

当请求到达您的服务器时,所有路由一次评估一个,并选择第一个匹配的路由来处理请求。

这是在控制器Nestjs和如何处理Nestjs的方法排序错误的副本@Get()装饰器?

最新更新