【问题标题】:How to assert exceptions with FluentAssertions version 4.x?如何使用 FluentAssertions 4.x 版断言异常?
【发布时间】:2020-02-21 15:06:00
【问题描述】:

我正在研究使用FluentAssertions-4.8.0 的更大解决方案。

由于我目前没有时间升级到最新版本(撰写本文时为 5.9.0),我想知道如何在上述版本中声明异常。

我知道它在 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("...");
}

【问题讨论】:

标签: c# exception xunit fluent-assertions


【解决方案1】:

回答我自己的问题。就这么简单:

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

【讨论】:

    猜你喜欢
    • 2021-10-27
    • 2022-01-17
    • 2019-06-04
    • 2020-05-31
    • 2020-10-25
    • 2021-04-06
    • 2014-04-08
    • 2017-12-14
    • 1970-01-01
    相关资源
    最近更新 更多