1

I'd like to assert that my dto (which is flat object with few properties) is equivalent to my entity (which is complex object with nested properties).

public class Dto 
{ 
      public string A {get;set;} 
      public string B {get;set;}
}
public class Entity
{ 
      public string A {get;set;} 
      public NestedObject C {get;set;}
      public string D {get;set;}
      public class NestedObject 
      {
             public string B {get;set;}
      }
}
Dto Entity
A A
B C.B
- D

If I try to do this:

dto.Should().BeEquivalentTo(entity)

it will throw an exception that Dto doesn't have "D" property. I don't want it to throw this exception.

entity.Should().BeEquivalentTo(dto)

this will not check nested objects and fail.

dto.Should().BeEquivalentTo(entity, x => x.ExcludeMissingProperties())

this will not throw an exception if dto have some property (for example, property "E") that does not map to an entity (entity does not have property "E"). I want it to throw an exception in that case.

How can I accomplish this? Thanks.

Shumek
  • 56
  • 4
  • It's unclear what `C.B` represents in your table. I recommend writing a more complete minimal example, in C#, including the class definitions. – Tullo_x86 Aug 03 '22 at 14:44
  • Sure, I've updated the question – Shumek Aug 03 '22 at 17:40
  • 1
    You might want to have a look at [this](https://stackoverflow.com/help/minimal-reproducible-example) to see what a complete minimal example should include. For this question a _complete_ block of code that we can paste into an IDE and run would make it much easier to follow your question. – Jonas Nyrup Aug 07 '22 at 15:00

0 Answers0