Matthias Güntert
Matthias Güntert

Reputation: 4638

How to assert exceptions with FluentAssertions version 4.x?

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("...");
}

Upvotes: 0

Views: 2515

Answers (1)

Matthias G&#252;ntert
Matthias G&#252;ntert

Reputation: 4638

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("...");
}

Upvotes: 4

Related Questions