我正在使用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" });
}
答案 0 :(得分:0)
没有优雅的方式,但你可以使用谓词:
[Test]
public void ShouldContainStudent()
{
new[] { new Student { Name = "George" }, new Student { Name = "Anna" } }
.Should().Contain(s => s.Name == "Anna");
}