【问题标题】:Is there a more appropriate to test if the constructor throws an exception?如果构造函数抛出异常,是否有更合适的测试?
【发布时间】:2012-06-09 15:38:30
【问题描述】:

通常你会测试,如果在某个方法中抛出异常,如下所示。 我使用 FluentAssertions

[Fact]
public void Exception_gets_thrown()
{
    // Arrange
    var foo = new Foo("validArgument");

    // Act/Assert
    foo.Invoking(f => f.Bar(null))            // null is an invalid argument
       .ShouldThrow<ArgumentNullException>();
}

但是如何测试,如果在构造函数中抛出异常? 我只是这样做了,但是否有更合适的方法 通过 FluentAssertions

[Fact]
public void Constructor_throws_Exception()
{
    // Arrange
    Action a = () => new Foo(null);         // null is an invalid argument

    // Act/Assert
    a.ShouldThrow<ArgumentNullException>();
}

【问题讨论】:

    标签: c# .net unit-testing xunit.net fluent-assertions


    【解决方案1】:

    这正是您应该测试异常的方式,这也是 ShouldThrow&lt;T&gt;()ShouldNotThrow&lt;T&gt;() 最初设计的目的。事实上,Invoking() 方法可能会在下一个大版本(2.0.0)中被标记为过时。

    【讨论】:

    • 对废弃 Invoking() 方法感到羞耻。我发现它比上面第二个测试中显示的 Action 机制更容易阅读。调用将所有内容很好地结合在一起,因此意图很明显。
    • @JackHughes 我们引入 Invoking() 和 ShouldThrow() 变体的原因是为了摆脱那些 [ExpectedException] 属性。 Metro 应用程序的单元测试项目甚至不再支持该属性。
    • Invoking 构造是一个不错的 API 恕我直言 :)
    • @DennisDomen - 这就是客户服务!! :)
    • 我们即将发布 2.1,它还在 ;-)
    【解决方案2】:

    我在测试构造函数时添加了一个如下所示的辅助方法:

    static Action Constructor<T>(Func<T> func)
    {
        return () => func();
    }
    

    然后我像这样使用它:

    Constructor(() => new Foo("bar", null))
    .ShouldThrow<ArgumentNullException>()
    .And
    .ParamName
    .Should()
    .Be("baz");
    

    我知道这是个人喜好问题,但我发现这比需要先声明和分配代表要干净一些。

    这将使原始问题中的代码如下所示:

    [Fact]
    public void Constructor_throws_Exception()
    {    
        // Act/Assert
        Constructor(() => new Foo(null)).ShouldThrow<ArgumentNullException>();
    }
    

    【讨论】:

      【解决方案3】:

      有一个内置的。

      FluentActions.Invoking(() => new Foo(null)).ShouldThrow<ArgumentNullException>();
      

      【讨论】:

        猜你喜欢
        • 2010-09-09
        • 2019-11-26
        • 1970-01-01
        • 1970-01-01
        • 2017-11-07
        • 2011-11-04
        • 2010-11-14
        • 1970-01-01
        • 2016-06-16
        相关资源
        最近更新 更多