简体   繁体   English

如何使用 FluentAssertions 4.x 版断言异常?

[英]How to assert exceptions with FluentAssertions version 4.x?

I am working on a larger solution which is using FluentAssertions-4.8.0 .我正在研究使用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.由于我目前没有时间升级到最新版本(撰写本文时为 5.9.0),我想知道如何在上述版本中声明异常。

I know how its getting done in 5.x, but how would I assert an exception in 4.x?我知道它在 5.x 中是如何完成的,但是我如何在 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("...");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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