c# - 在 fluentassertion 上调用异步任务

标签 c# fluent-assertions

可能是一个简单的,但无法让它工作;

我已将方法上的签名一更改为任务

在我的单元测试中,我使用了流畅的断言。

但无法让它工作:

        _catalogVehicleMapper
            .Invoking(m => m.MapToCatalogVehiclesAsync(searchResult, filtersInfo, request, default(CancellationToken)))
            .Should().Throw<ArgumentException>()
            .WithMessage("One of passed arguments has null value in mapping path and can not be mapped");

MapToCatalogVehiclesAsync 是异步方法,但我需要等待它,但等待和异步调用似乎并没有做到。 有人……?

最佳答案

虽然 Fabio 的回答也是正确的,但你也可以这样做:

_catalogVehicleMapper
   .Awaiting(m => m.MapToCatalogVehiclesAsync(searchResult, filtersInfo, request, default(CancellationToken)))
   .Should().Throw<ArgumentException>()
   .WithMessage("One of passed arguments has null value in mapping path and can not be mapped");

作为旁注,我建议始终在 WithMessage 中使用通配符。参见 this blog post 中的第 10 点.

关于c# - 在 fluentassertion 上调用异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51661681/

相关文章:

c# - 如何将 EventArgs 传递给 DelegateCommand?

c# - 使用 FluentAssertions 比较两个包含不同类型的字典集合

c# - 如何模拟 "Throws"和 "ThrowsAsync"中的异常类型

c# - FluentAssertions Should().BeEquivalentTo() 失败,列表包含从相同接口(interface)派生的运行时指定类型

c# - Windows API 在 WPF 中不起作用?

c# - 在 C# 中对文件进行数字签名时如何添加时间戳?

C# 等待 while 循环的一部分完成以执行另一个操作

c# - 在基本 Controller MVC 中实现逻辑

c# - 配置流利的断言精度以比较小数

c# - 流利的断言 : Assert one OR another value