I have a service method which gets data from the repository and using automapper I am mapping it to the dto.
public CustResponse GetCustomer(string id)
{
var repo = _myRepo.GetData(id);
var response = AutoMapper.Mapper.Map<CustResponse>(repo.custObj)
.Map(repo.addrObj);
return response;
}
I have created .map extension method in order to map multiple objects in class to a single dto.
public static TDestination Map<TSource, TDestination>(this TDestination destination, TSource source)
{
return Mapper.Map(source, destination);
}
Automapper configuration:
m.CreateMap<CUSTOBJ,CustResponse>().ForMember(/*Some Properties*/);
m.CreateMap<ADDROBJ,CustResponse>().ForMember(/*Some Properties*/);
Now, I would like to write a unit test case for my GetCustomer
method. I am not able to do so because my method depends on Mapper.Map
.
Is there anyway I can move var response = AutoMapper.Mapper.Map<CustResponse>(repo.custObj).Map(repo.addrObj);
to some interface and use the interface object to invoke the action.
In this way, I would be able to mock
auto mapper interface.
Unit Testing code:
var repStub = new Mock<ICustomerRepository>();
var expected = new CustResponse();
var mockMapper = new Mock<Mapper.Map>(); //Not working
var customer = new CustomerService(repStub.Object);
customer.GetCustomer("data");
Edit: I am not asking if I should do automapper unit testing or not. My questions my current service method does not allow me to create moq
objects of Mapper
. How can I create IMapper
interface with my implementation?