在MS C#单元测试中,如何断言发生了ArgumentException?

Zac*_*ott 7 unit-testing exception-handling

如果我们做了

throw new ArgumentException("Cannot do that");
Run Code Online (Sandbox Code Playgroud)

你如何断言ArgumentException微软的测试框架发生了这种情况?

Dar*_*rov 12

您可以使用以下[ExpectedException]属性修饰单元测试:

[ExpectedException(typeof(ArgumentException))]
[TestMethod]
public void Foo()
{
    throw new ArgumentException("foo");
}
Run Code Online (Sandbox Code Playgroud)

不要问关于声明异常消息:-)