1

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?

johnny 5
  • 19,893
  • 50
  • 121
  • 195

2 Answers2

1

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

Dennis Doomen
  • 8,368
  • 1
  • 32
  • 44
  • 1
    It's been a while I haven't check but did this feature get implemented yet? – johnny 5 Feb 27 '19 at 21:12
  • 1
    Nope. Some contributor took a stab at it, but it seems to be less trivial than I initially thought. See https://github.com/fluentassertions/fluentassertions/issues/535 – Dennis Doomen Mar 01 '19 at 19:17
0

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 }));
Tullo_x86
  • 2,573
  • 25
  • 27
  • What where is the synonym map? – johnny 5 Feb 25 '19 at 22:34
  • My apologies, I misread the question. The best I've been able to do is project using Linq: `subjectCollection.Should().BeEquivalentTo(expectedCollection.Select(o => new { Id = o.Key }))` – Tullo_x86 Feb 27 '19 at 20:28