如何测试应该永远不会被执行的代码? [英] How do I test code that should never be executed?

查看:193
本文介绍了如何测试应该永远不会被执行的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下方法应仅被调用。如何测试,覆盖罚球直插下面的代码片段?我知道,一个方法可能是在 VerifyThereAreInvalidiDigits 这种方法融合在一起。我正在寻找任何其他的想法。

Following method shall only be called if it has been verified that there are invalid digits (by calling another method). How can I test-cover the throw-line in the following snippet? I know that one way could be to merge together the VerifyThereAreInvalidiDigits and this method. I'm looking for any other ideas.

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.");
    }
}



我也不想写一个单元测试,锻炼应该永远不会被执行的代码。

I also would not want to write a unit test that exercises code that should never be executed.

推荐答案

如果在扔在问题的提出是真正的无法访问的在任何可能的情况下就应该可以的删除的并替换为:

If the "throw" statement in question is truly unreachable under any possible scenario then it should be deleted and replaced with:

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

如果代码可达然后写一个单元测试,测试这种情况。公共访问的方法错误报告的情况是完全有效的方案。你必须正确地处理所有的输入,甚至是坏的投入。如果做的正确的事情是抛出一个异常,然后测试你抛出一个异常

If the code is reachable then write a unit test that tests that scenario. Error-reporting scenarios for public-accessible methods are perfectly valid scenarios. You have to handle all inputs correctly, even bad inputs. If the correct thing to do is to throw an exception then test that you are throwing an exception.

更新:根据注释,它实际上是不可能的错误待击,因此代码不可达。但现在的Debug.Fail无法到达或者,它不会编译,因为编译器指出,一个返回值的方法有一个到达终点。

UPDATE: according to the comments, it is in fact impossible for the error to be hit and therefore the code is unreachable. But now the Debug.Fail is not reachable either, and it doesn't compile because the compiler notes that a method that returns a value has a reachable end point.

第一个问题不是实际上应该是一个问题;想必代码覆盖工具应该是可配置的忽视可达只调试代码。

The first problem should not actually be a problem; surely the code coverage tool ought to be configurable to ignore unreachable debug-only code. But both problem can be solved by rewriting the loop:

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++;
        } 
    }
}



另一种方法是重组代码,这样您不必摆在首位的问题:

An alternative approach would be to reorganize the code so that you don't have the problem in the first place:

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

和现在你不需要限制呼叫者首先调用AreThereInvalidDigits;只是使它合法调用此方法的任何时间。这似乎是比较安全的事。的方法炸毁当你没有做一些昂贵的检查,以确认他们是安全的呼吁是脆弱的,危险的方法。

and now you don't need to restrict the callers to call AreThereInvalidDigits first; just make it legal to call this method any time. That seems like the safer thing to do. Methods that blow up when you don't do some expensive check to verify that they are safe to call are fragile, dangerous methods.

这篇关于如何测试应该永远不会被执行的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆