FluentAssertions: Assert Collection contains Element that "IsEquivalentTo"

Its possible now. See accepted answer. Stop upvoting this workaround. ^^


It seems like I was too naive and there is in fact no method that does quite what I want to have. Thx @Nkosi for pointing out.

Just to round this topic up: We ended up with something like

objectList.Should().Contain(dto=>dto.Id == expectedDto.Id).Which.ShouldBeEquivalentTo(expectedDto)

This only works (edit: beautifully) when you have some kind of unique identifier (id, name, etc.). But at least it uses the build in ShouldBeEquivalentTo for all the other properties!

I created a feature request.

Thx for all the input!

BR Matthias


I finally had the time to implement this feature and it is now available with version 5.6.0 of FluentAssertions.

This now works!

var objectList = new List<SomeClass> { new SomeClass("A"), new SomeClass("B"), new SomeClass("C") };
objectList.Should().ContainEquivalentOf(new SomeClass("A"));

BR Matthias