使用 FluentAssertions 断言异步异常和 paramName

lon*_*nix 4 c# testing fluent-assertions

我在用着FluentAssertions

对于同步测试,我可以这样写:

action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("foo");
Run Code Online (Sandbox Code Playgroud)

对于异步测试,我这样做:

await action.Should().ThrowAsync<ArgumentNullException>();
Run Code Online (Sandbox Code Playgroud)

是否有一种方便的方法来断言ParamName,或者我必须通过包装在 try-catch 中手动完成?

Nko*_*osi 7

尝试捕获异常断言,您应该可以继续断言,就像使用同步代码一样。

//...

var error = await act.Should().ThrowAsync<ArgumentNullException>();

error.And.ParamName.Should().Be("foo");

//...
Run Code Online (Sandbox Code Playgroud)