C# ExpectedException 属性用法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19108107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:06:27  来源:igfitidea点击:

ExpectedException Attribute Usage

c#unit-testingexceptionexpected-exception

提问by Jonathan

I am trying to work with the ExpectedExceptionattribute in a C# UnitTest, but I am having issues getting it to work with my particular Exception. Here's what I got:

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

NOTE:I wrapped asterisks around the line that is giving me the trouble.

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

    [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);
        }
    }

I have the ExpectedExceptioncatching the base type Exception. Shouldn't this take care of it? I have tried using AttributeUsage, but it wasn't helping either. I know I can wrap it in a try/catch block, but I'd like to see if I can figure this style out.

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

Thanks all!

谢谢大家!

采纳答案by Mick

It will fail unless the type of exception is exactly the type you've specified in the attribute e.g

除非异常类型正是您在属性中指定的类型,否则它将失败,例如

PASS:-

经过:-

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

FAIL:-

失败:-

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

However this will pass ...

然而这会过去的...

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