Search code examples
c#testingfluent-assertions

Asserting async exception and paramName with FluentAssertions


I'm using FluentAssertions.

For a sync test, I can write this:

action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("foo");

For an async test, I do this:

await action.Should().ThrowAsync<ArgumentNullException>();

Is there a convenience method to also assert ParamName, or must I do it manually by wrapping in a try-catch?


Solution

  • Try capturing the exception assertion and you should be ale to continue the assertion as you would have with synchronous code.

    //...
    
    var error = await act.Should().ThrowAsync<ArgumentNullException>();
    
    error.And.ParamName.Should().Be("foo");
    
    //...