我如何测试我的 if 语句以便它被代码覆盖?

发布于 2025-01-09 06:14:26 字数 365 浏览 2 评论 0原文

我有这个函数

testing(x) {
      if(x == 10) {
        return 10;
      }
  }

,所以我的测试看起来像这样

 it('testing function', () => {
    expect(component.testing(10)).toBe(10);
  })

并且通过了,但问题是当我运行 code-coverage 时,我看到代码覆盖率由于这个 if 语句而减少了 - 所以我不能找到一种方法我该如何测试 这个 if 语句所以 branch 覆盖率就可以了

I have this function

testing(x) {
      if(x == 10) {
        return 10;
      }
  }

so my test looks like this

 it('testing function', () => {
    expect(component.testing(10)).toBe(10);
  })

and it passes but the problem is that when i run code-coverage than i see that code coverage is descresed because of this if statement - so i can't find a way how can i test
this if statement so the branch coverage will be fine

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

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

发布评论

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

评论(1

∞梦里开花 2025-01-16 06:14:26

您需要测试 x 何时不为 10 才能获得完整的代码覆盖率。

You'll need to test for when x is not 10 to get full code coverage.

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