johnny 5
johnny 5

Reputation: 20995

Comparing Collections Fluent Assertions Should All Be Equivalent To Synonyms

I'm using Fluent Assertions to Validate to different test Objects

public class DTO
{
   public int Key {get; set;}
   public string Code { get; set; }
}

public class KeyDTO
{
   public int Id {get; set;}
   public string Code { get; set; }
}

Note: this is not an exact replica of the code there are more field in the Original DTO but they're not necessary to explain the problems

I'm creating a function to assert that they are equal I'm trying use fluent assertions to do so. I Can't figure out a way to say that the Id Maps To the Key.

public void AssertDTOsAreEqual( List<DTO> orderedDTOs, List<KeyDTO> orderedKeys)
{        
    orderedDTOs.ShouldAllBeEquivalentTo(orderedKeys, o => o/*??*/)
}

Note: I Know as an alternative I can do this by zipping the ordered collections and comparing each property, but for more lengthy DTO's this would be trouble doing compairisons for each property.

Does anyone know of a way to map different properties in the ShouldAllBeEquivalentTo. Or Perhaps a better way to do this in general?

Upvotes: 1

Views: 2824

Answers (2)

Tullo_x86
Tullo_x86

Reputation: 2673

subjectCollection.Should().AllBeEquivalentTo(expected) has now been implemented in FluentAssertions:

https://fluentassertions.com/documentation/#collections-and-dictionaries

My apologies, I misread the question. The best I can come up with in the current version of FluentAssertions is to project the expected collection using Linq's .Select and compare to the new objects:

subjectCollection.Should().BeEquivalentTo(expectedCollection.Select(o => new { Id = o.Key }));

Upvotes: 0

Dennis Doomen
Dennis Doomen

Reputation: 8889

Unfortunately not yet. But this my personal number one on my list of features to add. I hope to get some time soon.

Upvotes: 1

Related Questions