2

I am using Fluent Assertions and willing to test if my collection contains some object using deep object graph comparison. I don't want to implement all that equality members. However, I can not find the way to do the test for equivalence containment of some object in the collection. For example, this test fails and I want it to pass:

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" });
}

Is there some elegant way to do it? Something like this?

[Test]
public void ShouldContainStudent()
{
    new[] { new Student { Name = "George" }, new Student { Name = "Anna" } }.ShouldContainEquivalent(new Student { Name = "Anna" });
}
Oleksandr Nechai
  • 1,811
  • 16
  • 27
  • You can't do that today, but technically it shouldn't be that complicated to make it possible. Most of the internals are already composable enough. – Dennis Doomen Mar 03 '17 at 10:06

1 Answers1

0

No elegant way, but you can use predicate:

[Test]
public void ShouldContainStudent()
{
    new[] { new Student { Name = "George" }, new Student { Name = "Anna" } }
        .Should().Contain(s => s.Name == "Anna");
}
Alexander Goldabin
  • 1,824
  • 15
  • 17