如何测试永远不会执行的代码?

THX*_*138 9 c# testing unit-testing code-coverage

只有在验证存在无效数字(通过调用另一种方法)时,才会调用以下方法.如何throw在以下代码段中测试-line?我知道一种方法可能是将VerifyThereAreInvalidiDigits这种方法合并在一起.我正在寻找任何其他想法.

public int FirstInvalidDigitPosition {
    get {
        for (int index = 0; index < this.positions.Count; ++index) {
            if (!this.positions[index].Valid) return index;
        }
        throw new InvalidOperationException("Attempt to get invalid digit position whene there are no invalid digits.");
    }
}
Run Code Online (Sandbox Code Playgroud)

我也不想写一个单元测试来运行永远不会被执行的代码.

Eri*_*ert 22

如果有问题的"throw"语句在任何可能的情况下都无法访问,则应将其删除并替换为:

Debug.Fail("This should be unreachable; please find and fix the bug that caused this to be reached.");
Run Code Online (Sandbox Code Playgroud)

如果代码可以访问,那么编写一个测试该场景的单元测试.公共可访问方法的错误报告方案是完全有效的方案.您必须正确处理所有输入,甚至输入错误.如果正确的做法是抛出异常然后测试你是否抛出异常.

更新:根据评论,实际上不可能命中错误,因此代码无法访问.但现在的Debug.Fail不可达要么,它不编译因为编译器指出,一个返回值的方法有到达终点.

第一个问题实际上不应该是一个问题; 当然,代码覆盖率工具应该是可配置的,以忽略无法访问的仅调试代码.但是这两个问题都可以通过重写循环来解决:

public int FirstInvalidDigitPosition 
{ 
    get 
    { 
        int index = 0;
        while(true) 
        {
            Debug.Assert(index < this.positions.Length, "Attempt to get invalid digit position but there are no invalid digits!");
            if (!this.positions[index].Valid) return index; 
            index++;
        } 
    }
}
Run Code Online (Sandbox Code Playgroud)

另一种方法是重新组织代码,以便您首先没有问题:

public int? FirstInvalidDigitPosition { 
    get { 
        for (int index = 0; index < this.positions.Count; ++index) { 
            if (!this.positions[index].Valid) return index; 
        } 
        return null;
    } 
} 
Run Code Online (Sandbox Code Playgroud)

现在你不需要限制调用者首先调用AreThereInvalidDigits; 只是让任何时候调用此方法合法.这似乎是更安全的事情.当你不做一些昂贵的检查来验证他们可以安全地打电话时爆炸的方法是脆弱,危险的方法.