Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 679
anastaciu Avatar asked Oct 15 '25 11:10

anastaciu


2 Answers

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

await act.Should().ThrowExactlyAsync<ArgumentException>();
like image 128
DavidG Avatar answered Oct 17 '25 23:10

DavidG


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

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

like image 35
quain Avatar answered Oct 18 '25 00:10

quain