title: Mock AutoMapper Mapper.Map call using Moq
body: <p>Whats the best way to setup a mock expection for the Map function in AutoMapper.</p>
  
  <p>I extract the IMapper interface so I can setup expects for that interface. My mapper has dependencies, so I have to pass those in to the mapper.</p>
  
  <p>What happens when I create 2 instances of my mapper class, with 2 different dependency implementations? I asume that both mappers will use the same dependency instance, since the AutoMapper map is static. Or AutoMapper might even throw an exception because I try to setup 2 different maps with the same objects.?</p>
  
  <p>Whats the best way to solve this?</p>
  
  <pre><code>public interface IMapper {
      TTarget Map&lt;TSource, TTarget&gt;(TSource source);
      void ValidateMappingConfiguration();
  }
  
  public class MyMapper : IMapper {
      private readonly IMyService service;
  
      public MyMapper(IMyService service) {
          this.service = service
          Mapper.CreateMap&lt;MyModelClass, MyDTO&gt;()
              .ForMember(d =&gt; d.RelatedData, o =&gt; o.MapFrom(s =&gt;
                  service.getData(s.id).RelatedData))
      }
  
      public void ValidateMappingConfiguration() {
          Mapper.AssertConfigurationIsValid();
      }
  
      public TTarget Map&lt;TSource, TTarget&gt;(TSource source) {
          return Mapper.Map&lt;TSource, TTarget&gt;(source);
      }
  }
  </code></pre>