Tag: 应该是##

应该测试exception消息

有没有办法用shouldly测试exception消息? 一个例子: public class MyException: Exception{ } 要测试的方法: public class ClassUnderTest { public void DoSomething() { throw new MyException(“Message”); } } 我通常会以这种方式测试它: [TestMethod] public void Test() { try { new ClassUnderTest().DoSomething(); Assert.Fail(“Exception not thrown”); } catch(MyException me) { Assert.AreEqual(“Message”, me.Message); }catch(Exception e) Assert.Fail(“Wrong exception thrown”); } } 我应该现在可以测试是否抛出exception: [TestMethod] public void TestWithShouldly() { Should.ThrowException(() => […]