流利断言只保留

时间:2015-07-06 09:49:44

标签: fluent-assertions

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

例如,我试图使用lambda;

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

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

1 个答案:

答案 0 :(得分:2)

我很确定这应该有效。从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.");
    }