为什么我的控制器的代码覆盖率不足?

发布于 2025-01-09 00:57:25 字数 2011 浏览 1 评论 0原文

我的控制器中有以下方法。我该如何测试它?注释覆盖失败。实际上我无法在笑话文件中定义覆盖代码。

我应该在测试文件中添加什么参数并传递以覆盖它?

 @Post('upload')
    @UseInterceptors(
        FileInterceptor('image', {
            storage: diskStorage({
                destination: './uploads',
                filename(_, file, callback) {
                    const randomName = Array(32)
                        .fill(null)
                        .map(() => Math.round(Math.random() * 16).toString(16))
                        .join('');
                    return callback(null, `${randomName}${extname(file.originalname)}`);
                },
            }),
        })
    )
    uploadFile(@UploadedFile() file) {
        return {
            url: `http://localhost:2300/api/inventory/uploads/${file.filename}`,
            originalFileName: `${file.originalname}`,
        };
    }

它工作得很好。我为此编写了这个笑话测试用例。

   it('Single file upload', async () => {
            const response = await controller.uploadFile(mockFile);
            const fileService = new InventoryController(service);
            expect(response).toBeTruthy();
            expect(response).toStrictEqual(
            expect.objectContaining({
                        originalFileName: expect.any(String),
                        url: expect.any(String),
                    }),
            );
        });

这是成功的,但该线路的覆盖范围失败。

 @UseInterceptors(
            FileInterceptor('image', {
                storage: diskStorage({
                    destination: './uploads',
                    filename(_, file, callback) {
                        const randomName = Array(32)
                            .fill(null)
                            .map(() => Math.round(Math.random() * 16).toString(16))
                            .join('');
                        return callback(null, `${randomName}${extname(file.originalname)}`);
                    },
                }),
            })
        )
       

I have below method in my controller. How can I test it? Annotation coverage is failing. Actually I am not able to define code for coverage in jest file.

What parameter I should add in my test file and pass to cover it?

 @Post('upload')
    @UseInterceptors(
        FileInterceptor('image', {
            storage: diskStorage({
                destination: './uploads',
                filename(_, file, callback) {
                    const randomName = Array(32)
                        .fill(null)
                        .map(() => Math.round(Math.random() * 16).toString(16))
                        .join('');
                    return callback(null, `${randomName}${extname(file.originalname)}`);
                },
            }),
        })
    )
    uploadFile(@UploadedFile() file) {
        return {
            url: `http://localhost:2300/api/inventory/uploads/${file.filename}`,
            originalFileName: `${file.originalname}`,
        };
    }

It is working perfectly. I wrote this jest test case for same.

   it('Single file upload', async () => {
            const response = await controller.uploadFile(mockFile);
            const fileService = new InventoryController(service);
            expect(response).toBeTruthy();
            expect(response).toStrictEqual(
            expect.objectContaining({
                        originalFileName: expect.any(String),
                        url: expect.any(String),
                    }),
            );
        });

This is successful but coverage is failing for this line.

 @UseInterceptors(
            FileInterceptor('image', {
                storage: diskStorage({
                    destination: './uploads',
                    filename(_, file, callback) {
                        const randomName = Array(32)
                            .fill(null)
                            .map(() => Math.round(Math.random() * 16).toString(16))
                            .join('');
                        return callback(null, `${randomName}${extname(file.originalname)}`);
                    },
                }),
            })
        )
       

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哽咽笑 2025-01-16 00:57:25

如果代码覆盖率在单元测试的所有行上都是绝对必须的,我在这里要做的就是将 filename 函数移动到导出该方法的单独文件中,以便可以直接测试它。

在单元测试期间,不会调用增强器,只会创建增强器,因此除了最初的“是的,这个装饰器有效”类型的覆盖之外,您不会获得任何覆盖。对于这个内部回调,您需要一个单独的函数。

如果代码覆盖率不是那么重要的指标,另一种选择是在 e2e 测试中对此进行测试。这样你就可以确定这按预期工作

What I would do here if code coverage is an absolute must on all lines with unit tests, is move the filename function to a separate file that exports the method so it can be tested directly.

During unit tests, enhancers aren't called, only created, so you won't get any coverage other than the initial "yeah, this decorator works" kind of coverage. For this inner callback, you'll need a separate function.

The other option, if code coverage isn't as important a metric, would be to test this in an e2e test. That way you know for certain this works as intended

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文