【问题标题】:Fluent Assertions OnlyContain流利的断言仅包含
【发布时间】:2015-09-23 09:52:28
【问题描述】:

使用 FluentAssertions,我想检查一个列表是否只包含具有特定值的对象。

例如,我尝试使用 lambda;

myobject.Should().OnlyContain(x=>x.SomeProperty == "SomeValue");            

但是,这种语法是不允许的。

【问题讨论】:

  • 我找到了实现这一点的方法,但不确定是否有更好的方法; myobject.Select(x=>x.SomeProperty).Should().Equal("SomeValue")

标签: fluent-assertions


【解决方案1】:

我很确定这应该可行。查看 FluentAssertions 的 GitHub 存储库中的 this example 单元测试:

    [TestMethod]
    public void When_a_collection_contains_items_not_matching_a_predicate_it_should_throw()
    {
        //-----------------------------------------------------------------------------------------------------------
        // Arrange
        //-----------------------------------------------------------------------------------------------------------
        IEnumerable<int> collection = new[] { 2, 12, 3, 11, 2 };

        //-----------------------------------------------------------------------------------------------------------
        // Act
        //-----------------------------------------------------------------------------------------------------------
        Action act = () => collection.Should().OnlyContain(i => i <= 10, "10 is the maximum");

        //-----------------------------------------------------------------------------------------------------------
        // Act
        //-----------------------------------------------------------------------------------------------------------
        act.ShouldThrow<AssertFailedException>().WithMessage(
            "Expected collection to contain only items matching (i <= 10) because 10 is the maximum, but {12, 11} do(es) not match.");
    }

【讨论】:

    猜你喜欢
    • 2020-02-08
    • 2014-07-27
    • 2018-06-25
    • 2018-12-01
    • 2021-01-18
    • 2020-08-16
    • 2016-04-11
    • 2021-01-08
    • 2022-07-23
    相关资源
    最近更新 更多