使用FluentAssertions,我想检查一个只包含具有特定值的对象的列表。
例如,我尝试使用lambda;
myobject.Should().OnlyContain(x=>x.SomeProperty == "SomeValue");
但是,这种语法是不允许的。
我确信这应该有效。从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.");
}