在fluentassertion上调用异步任务

Roe*_*t M 2 c# fluent-assertions

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

我已经将方法上的签名改为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");
Run Code Online (Sandbox Code Playgroud)

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

Den*_*men 8

虽然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");
Run Code Online (Sandbox Code Playgroud)

作为旁注,我建议总是使用通配符WithMessage.请参阅此博客文章中的第 10点.