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

Time:11-18

The following Func delegate throws an ArgumentNullException:

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

Using Fluent assertions, the assertion:

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

Should fail:

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

ArgumentNullException derives from ArgumentException, given the description, the assertion should fail, but it passes.

Is it a bug or am I misusing this?

CodePudding user response:

Since ThrowExactlyAsync returns a Task, you're not actually doing anything unless you await it:

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

CodePudding user response:

Replace act.Should().ThrowExactlyAsync<ArgumentException>();
with await act.Should().ThrowExactlyAsync<ArgumentException>();\

This will give you a correct result (fail in this case).

  • Related