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