ExpectedException Attribute Usage

前端 未结 1 1542
北恋
北恋 2021-02-05 04:41

I am trying to work with the ExpectedException attribute in a C# UnitTest, but I am having issues getting it to work with my particular Exception

相关标签:
1条回答
  • 2021-02-05 04:56

    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;
        }
    
    0 讨论(0)
提交回复
热议问题