I am using AutoMapper and have a definition of mapping engine as
private readonly IMappingEngine _mappingEngine;
I initialize it via constructor injection and use in the code as below
var product=//Get a single product
var productModel = _mappingEngine.Map<ProductModel>(product);
The above works perfectly. I now need to map a list of Product
to list of ProductModel
The following works in the controller action
var entities =//Get list of products
var model = entities.Select(e => _mappingEngine.Map<ProductModel>(e));
The above LINQ code uses foreach and converts each Product
to a ProductModel
Now I need to unit test the above code but unable to use Moq to mock the LINQ statement above
I've tried the following
var mapper = new Mock<IMappingEngine>();
mapper.Setup(m => m.Map<ProductModel>(product)).Returns(ProductModel);
The above mapper setup works for single object mapping. How can we setup the above using a list of products
So, I want to be able to setup a list of Product
like this:
var productList=new List<Product>{new Product{Id=1,name="product 1"},
new Product{Id=2,name="product 2"}};
and define a mocking that will return a list of ProductModel
like this:
var productModelList=new List<ProductModel>{new ProductModel{Id=1,name="product 1"},
new ProductModel{Id=2,name="product 2"}};
when my test calls the controller (which uses the mock IMappingEngine
to transform the list)
var model = entities.Select(e => _mappingEngine.Map<ProductModel>(e));
So, when writing Moq unit tests how can we setup the above so that _mappingEngine.Map
that takes productList
as input and returns productModelList
?