【发布时间】:2017-06-23 09:38:38
【问题描述】:
新的 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 提供的方法不足以识别引发异常的参数,所以这是一个不同的问题。
【问题讨论】:
-
如果您能展示一个您尝试移植的测试的清晰示例,这将非常有帮助。目前我还不清楚你的情况。
-
请使用minimal reproducible example 编辑您的问题,而不是将其放入 cmets。
-
NUnit: Assert.Throws的可能重复
标签: c# nunit-3.0 expected-exception