新的 NUnit 版本 3.x 不再支持ExpectedExceptionAttribute
。有一个Assert.Throws<MyException>()
代替。可能是一个更好的逻辑概念。但我没有找到任何旧货的替代品MatchType
——有吗?MyException
可以用许多参数抛出,在 NUnit 2.x 中,我可以比较包含某个文本片段的异常消息,以了解使用了哪个参数(当然,我不会有几十个异常类合乎逻辑的)。NUnit 3.x 如何处理这个问题?我找不到提示。
使用 NUnit 2.x,我将执行以下操作:
[Test]
[ExpectedException(ExpectedException=typeof(MyException), ExpectedMessage="NON_EXISTENT_KEY", MatchType=MessageMatch.Contains)]
public void DeletePatient_PatientExists_Succeeds()
{
Person p = new Person("P12345", "Testmann^Theo", new DateTime(1960, 11, 5), Gender.Male);
MyDatabase.Insert(p);
MyDatabase.Delete(p.Key);
// Attemp to select from a database with a non-existent key.
// MyDatabase throws an exception of type MyException with "NON_EXISTENT_KEY" within the message string,
// so that I can distinguish it from cases where MyException is thrown with different message strings.
Person p1 = MyDatabase.Select(p.Key);
}
如何使用 NUnt 3.x 做类似的事情?
请考虑我的意思:NUnit 提供的方法不足以识别引发异常的参数,所以这是一个不同的问题。