6
有没有办法用异常地测试异常消息?用异常消息测试异常消息
一个例子:
public class MyException: Exception{
}
的方法进行测试:
[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");
}
}
随着shouldly我现在可以测试是否:
public class ClassUnderTest
{
public void DoSomething()
{
throw new MyException("Message");
}
}
我通常以这种方式测试该抛出异常:
[TestMethod]
public void TestWithShouldly()
{
Should.ThrowException<MyException>(() => new ClassUnderTest().DoSomething());
}
但我该如何测试异常的消息?
该死的,这么简单。应该自己想清楚了。谢谢 – Steffen
显然,在最新版本中它应该是'Throw' –
Noctis