[英]Why does this else condition not meet code coverage?
I'm confused about why the following if/else
condition doesn't meet code coverage.我对为什么以下if/else
条件不满足代码覆盖率感到困惑。 Specifically the else
is getting flagged as not having coverage.特别是else
被标记为没有覆盖。
if (myBoolean) {
myComponent.myElement = 'Hello world';
} else {
myComponent.myElement = 'Hello everyone';
}
I can refactor this to set the value of myComponent.myElement
prior to the if
condition, and that seem to pass fine.我可以重构它以在if
条件之前设置myComponent.myElement
的值,这似乎很好。
myComponent.myElement = 'Hello everyone';
if (myBoolean) {
myComponent.myElement = 'Hello world';
}
Why wouldn't the else
condition pass as well?为什么else
条件也不能通过?
The coverage tool records which lines of code your tests cause to be executed.覆盖工具记录您的测试导致执行的代码行。
If your flag is set by reading some input, then the flag is used to decide which branch to take, the test will follow either one path or the other.如果您的标志是通过读取某些输入来设置的,那么该标志将用于决定采用哪个分支,测试将遵循一条路径或另一条路径。 If your code under test checks for invalid inputs, code handling that won't be executed by a test using valid inputs.如果您的测试代码检查无效输入,则使用有效输入的测试不会执行代码处理。 You will need a test for each possible case so that all the paths get covered.您将需要对每种可能的情况进行测试,以便涵盖所有路径。
By the way, the coverage tool is useful not just for the statistics it generates but for confirming the test does what you think it does.顺便说一句,覆盖率工具不仅对它生成的统计数据很有用,而且对确认测试是否按照您的想法行事很有用。 If a test fails you can look at the coverage and see how far it got and which branches were executed.如果测试失败,您可以查看覆盖率并查看它达到了多远以及执行了哪些分支。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.