0

I am working on a larger solution which is using FluentAssertions-4.8.0.

As I currently don't have time to upgrade to the latest version (5.9.0 as of writing) I would like to know how to assert exceptions in the mentioned version.

I know how its getting done in 5.x, but how would I assert an exception in 4.x?

[Fact]
public void Should_Throw_InvalidOperationException_If_...()
{
    // Arrange
    var resolver = new SomeResolver();
    var foo = new Foo();

    Action act = () => resolver.DoSomething(foo);

    // Act & Assert     
    act.Should().Throw<InvalidOperationException>.WithMessage("...");
}
Matthias Güntert
  • 4,013
  • 6
  • 41
  • 89

1 Answers1

3

To answer my own question. It's as simple as this:

[Fact]
public void Should_Throw_InvalidOperationException_If_...()
{
    // Arrange
    var resolver = new SomeResolver();
    var foo = new Foo();

    Action act = () => resolver.DoSomething(foo);

    // Act & Assert     
    act.ShouldThrow<InvalidOperationException>().WithMessage("...");
}
Matthias Güntert
  • 4,013
  • 6
  • 41
  • 89