3

I'm trying to use Assert.AreEqual in a test to validate a DTO from our provider.

The assert is failing with:

Expected: DTO.EmployeeDTO

Actual: DataProviders.Mappers.EmployeeMapper

Is there something I can do with our DTO and/or mapper to get AreEqual working? Should I override Equals for the DTO and use Assert.Equal instead?

Here is our expected employee DTO for comparison:

EmployeeDTO expected = new EmployeeDTO
{
    Category = "OPS", 
    Code = "EMPL",
    Email = "[email protected]",
    JobDescription = "Philanthropist",
    FirstName = "Bill",
    Bolander = "Gates",
    ResourceID = 1234567,
    ResourceNumber = "ABCD1234567",
    UserGUID = Guid.Parse("0A76A348-B709-9EF0-9E44-419433E7C90D"),
    UserName = "billygates"
};

Here is our call from the data provider, which should return an EmployeeDTO that we can compare to our expected DTO.

 EmployeeDTO actual = controller.GetEmployee("billygates");

Here is the GetEmployee provider method which should return an employee DTO:

public EmployeeDTO GetEmployee(string userName)
{
    EmployeeDTO toReturn = null;

    //Get employee   
    toReturn = (from employees in Context.EmployeeTable
       where employees.UserName.Equals(userName)
       select new EmployeeMapper
       {
        MapToFullDTO = employees
       }
       ).SingleOrDefault<EmployeeDTO>();
}

Note that the MapToFullDTO property is part of a class called EmployeeMapper which inherits from EmployeeDTO. This property populates the base classes' properties for convenience purposes so we don't have to write out the mapping every time. I understand this is part of the issue, but I am not sure why.

2
  • 1
    I'd use Extension Methods syntax here, e.g. Context.EmployeeTable.Where(e => e.UserName.Equals(userName)) .Select(e => new EmployeeMapper { apToFullDTO = e }) to do not mess with Query Syntax. .SingleOrDefault<EmployeeDTO>(). Commented Nov 7, 2013 at 23:29
  • 1
    Just a side comment totally unrelated to your question, I'm about syntax and styles. You mess up in the LINQ query two syntaxes: so called "query" and "extension methods". For readability it's better to stick with one. Commented Nov 8, 2013 at 0:20

1 Answer 1

6

You need to override Equals() method on the class to compare the objects based on properties' values.

Otherwise the comparison will compare the references of the objects which are obviously different.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.