Fluent Assertions:检查集合是否包含对象,等同于提供的对象?

时间:2017-03-01 13:53:09

标签: fluent-assertions

我正在使用Fluent Assertions并愿意使用深度对象图比较来测试我的集合是否包含一些对象。我不想实现所有那些平等成员。但是,我找不到对集合中某些对象的等价包含进行测试的方法。例如,此测试失败,我希望它通过:

class Student
{
    public string Name { get; set; }
}

[Test]
public void ShouldContainStudent()
{
    new[] { new Student { Name = "George" }, new Student { Name = "Anna" } }.Should()
        .Contain(new Student { Name = "Anna" });
}

有一些优雅的方法吗?像这样的东西?

[Test]
public void ShouldContainStudent()
{
    new[] { new Student { Name = "George" }, new Student { Name = "Anna" } }.ShouldContainEquivalent(new Student { Name = "Anna" });
}

1 个答案:

答案 0 :(得分:0)

没有优雅的方式,但你可以使用谓词:

[Test]
public void ShouldContainStudent()
{
    new[] { new Student { Name = "George" }, new Student { Name = "Anna" } }
        .Should().Contain(s => s.Name == "Anna");
}