3

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?

lonix
  • 14,255
  • 23
  • 85
  • 176

2 Answers2

6

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");

//...
Nkosi
  • 235,767
  • 35
  • 427
  • 472
0

You can make it fully fluent with WaitASync().Result:

act.Should().ThrowAsync<ArgumentNullException>().WaitAsync(CancellationToken.None).Result.And.ParamName.Should().Be("foo");
Sybren S
  • 11
  • 3