0

if i have a extension method that converts an Person object to a PersonDTO then

  1. should i unit test that the conversion occurs correctly
  2. if so and i use fluentassertions how do i assert that the conversion is correct

my extension method is:

public static PersonDTO ToDto (this Person person)
    {
        if (person == null) return null;
        return new PersonDTO
        {
            FirstName = person.FirstName,
            LastName = person.LastName,
            Dob = person.Dob,
            Identifier= person.Id
        };            
    }

my person object has other properties that are not being mapped.

to get the personDTO object i would therefore do something similar to below:

var newPerson = new Person{ //set properties here };
var personDto = newPerson.ToDto();
JimmyShoe
  • 2,009
  • 4
  • 21
  • 40

1 Answers1

1

First, yes, this code should be tested.

In order to check that the conversion is correct, you have to build the expected result manually, and then assert that the result of the function is the same:

var person = new Person{ ... };
var expectedPersonDto = new PersonDto{ ... };
person.ToDto().Should().BeEquivalentTo(expectedPersonDto);

See this for more information on comparing object graphs.

Kraylog
  • 7,383
  • 1
  • 24
  • 35
  • ok thanks - i thought that might be the case - creating the expected result could be quite tedius if objects has lot of properties. – JimmyShoe Jul 17 '19 at 11:48
  • Indeed, which is why you might want to create builders that create those objects. See [here](http://www.natpryce.com/articles/000714.html). – Kraylog Jul 17 '19 at 11:49
  • Or compare it an anonymous type that has the properties that you care about. FA will ignore the rest. However, I would not test those individual methods, but test it as part of the API that exposes it. – Dennis Doomen Jul 18 '19 at 05:51