1

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?

Shaggy
  • 5,422
  • 28
  • 98
  • 163
  • Possible duplicate of [Is it a good practice to mock Automapper in unit tests?](https://stackoverflow.com/questions/39998115/is-it-a-good-practice-to-mock-automapper-in-unit-tests) – cbp Oct 25 '18 at 22:19
  • See also: https://stackoverflow.com/questions/14177455/unit-test-the-automapper-profiles – cbp Oct 25 '18 at 22:21
  • @cbp : I don't think it is a duplicate question. What I want is to move my mapper method logic to the interface. As I have an extension method on top of it and I am not able to understand how to invoke it using interface object? – Shaggy Oct 25 '18 at 23:22
  • @Shaggy Don't tightly couple to the static API. register the `IMapper` with your container and inject where it is needed. From there you should be able to easily mock/stub a mapper when testing. – Nkosi Oct 25 '18 at 23:34
  • @Shaggy what platform? Core? – Nkosi Oct 25 '18 at 23:38
  • @Shaggy Also show a test as a [mcve]. Should be able to get help from there – Nkosi Oct 25 '18 at 23:42
  • @Nkosi : Yes, that's what I wanted to do. I am not able to write an interface for my implementation. I want a method which would allow me to use multiple `.Map` methods on `Mapper` object. I am using `.net 4.6` – Shaggy Oct 25 '18 at 23:48
  • Automapper already has an IMapper interface in the instance API http://docs.automapper.org/en/stable/Static-and-Instance-API.html – Nkosi Oct 25 '18 at 23:49
  • 1
    So you either refactor to use the instance API and inject that or make sure to configure the static API for your tests if coupled to static API. – Nkosi Oct 25 '18 at 23:54
  • 1
    @Shaggy check a previous answer I gave in this same space https://stackoverflow.com/a/39868221/5233410 – Nkosi Oct 26 '18 at 00:10

0 Answers0