C# 在fluentassertion上调用异步任务

C# 在fluentassertion上调用异步任务,c#,fluent-assertions,C#,Fluent Assertions,可能是一个简单的,但不能让它工作 我已将方法上的签名更改为Task 在我的单元测试中,我使用了流畅的断言 但这无法实现: _catalogVehicleMapper .Invoking(m => m.MapToCatalogVehiclesAsync(searchResult, filtersInfo, request, default(CancellationToken))) .Should().Throw<Argum

可能是一个简单的,但不能让它工作

我已将方法上的签名更改为Task

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

但这无法实现:

        _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");
\u目录车辆标签
.Invoking(m=>m.MapToCatalogVehiclesAsync(searchResult、filtersInfo、request、default(CancellationToken)))
.Should().Throw()
.WithMessage(“传递的参数之一在映射路径中具有空值,无法映射”);
MapToCatalogVehiclesAsync
是异步方法,但我需要等待它,但等待和异步调用似乎无法做到这一点。。 某人..?

调用
extensoin方法返回
操作
,使用异步方法时,该操作相当于
异步无效
——因此不会等待异常

作为解决方法,您可以将测试下的方法包装为
Func

[事实]
公共异步任务ThrowException()
{
函数运行=
()=>_catalogVehicleMapper.maptoCatalogVehicleSync(
搜索结果,
过滤信息,
要求
默认值(CancellationToken));
run.Should().Throw();
}

虽然法比奥的答案也是正确的,但您也可以这样做:

_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");
\u目录车辆标签
.waiting(m=>m.MapToCatalogVehiclesAsync(searchResult、filtersInfo、request、default(CancellationToken)))
.应该()扔

_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");