【问题标题】:ExpectedException Attribute UsageExpectedException 属性用法
【发布时间】:2013-10-01 04:39:56
【问题描述】:

我正在尝试使用C# UnitTest 中的ExpectedException 属性,但我无法让它与我的特定Exception 一起使用。这是我得到的:

注意:我在给我带来麻烦的行周围加上了星号。

    [ExpectedException(typeof(Exception))]
    public void TestSetCellContentsTwo()
    {
        // Create a new Spreadsheet instance for this test:
        SpreadSheet = new Spreadsheet();

        // If name is null then an InvalidNameException should be thrown. Assert that the correct 
        // exception was thrown.
        ReturnVal = SpreadSheet.SetCellContents(null, "String Text");
        **Assert.IsTrue(ReturnVal is InvalidNameException);**

        // If text is null then an ArgumentNullException should be thrown. Assert that the correct
        // exception was thrown.
        ReturnVal = SpreadSheet.SetCellContents("A1", (String) null);
        Assert.IsTrue(ReturnVal is ArgumentNullException);

        // If name is invalid then an InvalidNameException should be thrown. Assert that the correct 
        // exception was thrown.
        {
            ReturnVal = SpreadSheet.SetCellContents("25", "String Text");
            Assert.IsTrue(ReturnVal is InvalidNameException);

            ReturnVal = SpreadSheet.SetCellContents("2x", "String Text");
            Assert.IsTrue(ReturnVal is InvalidNameException);

            ReturnVal = SpreadSheet.SetCellContents("&", "String Text");
            Assert.IsTrue(ReturnVal is InvalidNameException);
        }
    }

我有 ExpectedException 捕获基本类型 Exception。这不应该管吗?我试过使用AttributeUsage,但也没有用。我知道我可以将它包装在 try/catch 块中,但我想看看我是否能弄清楚这种风格。

谢谢大家!

【问题讨论】:

    标签: c# unit-testing exception expected-exception


    【解决方案1】:

    除非异常类型与您在属性中指定的类型完全相同,否则它将失败 例如

    通过:-

        [TestMethod()]
        [ExpectedException(typeof(System.DivideByZeroException))]
        public void DivideTest()
        {
            int numerator = 4;
            int denominator = 0;
            int actual = numerator / denominator;
        }
    

    失败:-

        [TestMethod()]
        [ExpectedException(typeof(System.Exception))]
        public void DivideTest()
        {
            int numerator = 4;
            int denominator = 0;
            int actual = numerator / denominator;
        }
    

    不过这一切都会过去的……

        [TestMethod()]
        [ExpectedException(typeof(System.Exception), AllowDerivedTypes=true)]
        public void DivideTest()
        {
            int numerator = 4;
            int denominator = 0;
            int actual = numerator / denominator;
        }
    

    【讨论】:

    • 像魅力一样工作,感谢您的解释。这是一些简单的代表,干杯!
    • 我不鼓励 [TestMethod()] [ExpectedException(typeof(System.Exception), AllowDerivedTypes=true)] 出于同样的原因,我不鼓励 ... catch(Exception ex) {....
    • 难道我们不需要一个 try/catch 围绕预期的违规代码来捕获预期的异常吗?
    • @SuLlewellyn 捕获异常而不重新抛出将导致应用 ExpectedExceptionAttribute 时测试失败。
    猜你喜欢
    • 2013-02-07
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2014-05-24
    • 1970-01-01
    相关资源
    最近更新 更多