1

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

例如,我尝试使用 lambda;

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

但是,不允许使用此语法。

4

1 回答 1

2

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

    [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.");
    }
于 2015-07-07T10:33:39.727 回答