5

I'm not sure if an old version of FluentAssertions had this or not but I'd like to compare a collection to another collection. I have a dto like so:

public class UserDTO
{
    public int Id { get; set; }
    public string Username { get; set; }
}

I have two lists Id like to compare.

 List<UserDTO> createdUsers = this.GetCreatedUser();
 var expectedResults = this.dbContext.Users.Top(10);

The closest thing I see to should all be equivalent is:

 createdUsers.Should().AllBeEquivalentTo(expectedResults)

but when I try to pass my exclusions, it seems to be operating providing me exlusions for the list instead of the entity itself.

I would like to compare two list of these excluding the Id property. I could of sworn there was a function called ShouldAllBeEquivalentTo which took in options to allow exluding,

createdUsers.ShouldAllBeEquivalentTo(expectedResults, o => o.Excluding(x => x.Id);

How can I compare collections while excluding properties in the comparison?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
johnny 5
  • 19,893
  • 50
  • 121
  • 195

1 Answers1

6

Documentation suggests the following when it comes to exclusions with Collections and Dictionaries

createdUsers.Should().BeEquivalentTo(expectedResults, options => options.Excluding(_ => _.Id));

Quote from documentation:

to assert that all instances of OrderDto are structurally equal to a single object:

orderDtos.Should().AllBeEquivalentTo(singleOrder);

Reference Object graph comparison: Collections and Dictionaries

Arin Ghazarian
  • 5,105
  • 3
  • 23
  • 21
Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • Interesting, I was working on such an old version with my last company, things must have changed a bit, Thanks! – johnny 5 Jun 22 '18 at 01:41
  • This is correct. `BeAllEquivalentTo` would compare each object in the collection with the same single object. – Dennis Doomen Jun 22 '18 at 05:54
  • 2
    @johnny5 Yes, in the latest Major version of FA, `ShouldBeEquivalentTo()` was moved to `Should().BeEquivalentTo()` as stated on Dennis's blog: https://www.continuousimprover.com/2018/02/fluent-assertions-50-best-unit-test.html#upgrading-tips – Michal Ciechan Jun 22 '18 at 16:42