Ray
Ray

Reputation: 1442

Why does Assert.AreEqual Fail on <Object, Object>? Type Mismatch?

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.

Upvotes: 3

Views: 581

Answers (1)

Szymon
Szymon

Reputation: 43023

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.

Upvotes: 6

Related Questions