MatchType 的替代品?

Replacement for MatchType?

新的 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 提供的方法不足以识别引发异常的参数,因此这是一个不同的问题。

看起来,确实存在提供此功能的可能性(甚至比上述更明显),尽管不是在 NUnit 3 本身中,而是在 FluentAssertions (http://www.fluentassertions.com/) .在那里,你可以做

 Action act = () => MyDatabase.Select(p.Key);
 act.ShouldThrow<MyException>().Where(ex => ex.Message.Contains("NON_EXISTENT_KEY"));

出于我所有的实际目的,这解决了问题。

var ex = Assert.Throws<MyException>(()=> MyDatabase.Select(p.Key));
StringAssert.Contains("NON_EXISTENT_KEY", ex.Message);