【问题标题】:Asserting async exception and paramName with FluentAssertions使用 FluentAssertions 断言异步异常和 paramName
【发布时间】:2021-10-27 01:40:20
【问题描述】:

我正在使用FluentAssertions

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

action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("foo");

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

await action.Should().ThrowAsync<ArgumentNullException>();

是否有一种方便的方法也可以断言 ParamName,或者我必须通过包装在 try-catch 中手动执行它?

【问题讨论】:

  • 我不熟悉这个库,但我认为它类似于(await action.Should().ThrowAsync&lt;ArgumentNullException&gt;()).And.ParamName.Should().Be("foo");
  • @DavidL 是的,谢谢。
  • 从FA 6.0开始你也可以使用.WithParameterName("foo")

标签: c# testing fluent-assertions


【解决方案1】:

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

//...

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

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

//...

【讨论】:

    猜你喜欢
    • 2020-02-21
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 2012-09-14
    • 1970-01-01
    相关资源
    最近更新 更多