Search code examples
c#nunitautomappermstestidatareader

NUnit: Automapper<IDataReader, Dto>.ConvertUsing() returns Dto with null properties


I'm using AutoMapper to map from IDataReader to a simple DTO.

I'm able to map the properties when I use ForMember, but not when I use ConstructUsing/ConvertUsing. In this case, all my NUnit tests fail, because the AutoMapper returns a DTO with null properties. What's interesting is that this behavior does not occur in MSTest: When running the tests under MSTest, the mapping works.

Here's the code:

public class Dto
{
public string Name { get; set; }
public string Value { get; set; }
}

This passes in NUnit and in MSTest:

Mapper.CreateMap<IDataReader, Dto>()
.ForMember(x => x.Name, map => map.MapFrom(reader => reader["Name"]))
.ForMember(x => x.Value, map => map.MapFrom(reader => reader["Value"]));

This passes only in MSTest and returns Dto with null properties in NUnit:

Mapper.CreateMap<IDataReader, Dto>()
.ConvertUsing(Map); // ConstructUsing doesn't work either

private Dto Map(IDataReader reader)
{
    return new Dto
    {
         Name = (string)reader["Name"],
         Value = (string)reader["Value"]
    };
}

MyTestMethod is not even called in NUnit.

Is this a bug in AutoMapper? In NUnit? Both?

Should I not use AutoMapper for IDataReader mapping?

Thanks in advance.


Solution

  • After I accidentally stumbled upon this question, https://groups.google.com/forum/#!topic/automapper-users/3DcPbP-GgNg

    I figured out that this has nothing to do with NUnit/MsTest and was simply caused due to AutoMapper.Net4.DLL being in my project.

    This DLL contains DataReaderMapper, which overrode my custom IDataReader mapping. Since my real project columns did not match the object's properties names, DataReaderMapper simply returned null.

    (As for NUnit/MsTest, I had different projects, and didn't suspect the extra DLL in one of them. After narrowing the problem I was able to reproduce it on both frameworks.)

    Removing AutoMapper.Net4.DLL from my project solved the problem.

    Sorry for the misleading question :)