流畅的断言 Should().ThrowExactlyAsync 应该对于派生类型失败,但它不会

Fluent assertion Should().ThrowExactlyAsync should fail for derived types, but it doesn't

以下 Func 代表抛出 ArgumentNullException

Func<Task> act = async () => await _someService
            .someMethod(1, 2, 3, 4);

使用Fluent assertions,断言:

act.Should().ThrowExactlyAsync<ArgumentException>();

应该失败:

Asserts that the current Func throws an exception of the exact type TException (and not a derived exception type).

ArgumentNullException 派生自 ArgumentException,根据描述,断言应该失败,但它通过了。

这是一个错误还是我误用了它?

自从 ThrowExactlyAsync returns 一个 Task,你实际上并没有做任何事情,除非你 await 它:

await act.Should().ThrowExactlyAsync<ArgumentException>();

替换act.Should().ThrowExactlyAsync<ArgumentException>();
await act.Should().ThrowExactlyAsync<ArgumentException>();\

这会给你一个正确的结果(在这种情况下失败)。