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