如何使用
c#
和 Sonar
来做到这一点?
给出代码:
if (root?.hop1?.hop2.hop3?.value == "blah") // this condition is not covered as far as Sonar is concerned
{
do(); // this line is covered as far as Sonar is concerned
}
我们可以创建一个对象来测试
do()
,但是如何覆盖实际情况呢?仅提供“快乐”路径会使条件未被覆盖,我们是否应该提供构成不快乐路径的所有排列(这是一场噩梦,因为此代码正处于另一个测试的中间)或者您如何做到这一点?
我的答案是——在表达式中包含“?”的情况下被评估为一个值或使用独立的
if
分支进行检查,完全没有必要要求代表“提前退出”的值,它只会增加喧嚣。
我建议重构此类代码以使用类似
target.Evaluate<T>("hop1.hop2.hop3")
的内容。