简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM