title: How to Mock an AutoMapper IMapper object in Web API Tests With StructureMap Dependency Injection?
body: <p>So I've build a WebAPI from scratch, including some best practices that I've found online such as Dependency Injection and Domain&lt;-&gt;DTO mapping using auto mapper etc.</p>
  <p>My API Controllers now look similar to this</p>
  <pre class="lang-cs prettyprint-override"><code>public MyController(IMapper mapper)
  {
  }
  </code></pre>
  <p>and AutoMapper Registry:</p>
  <pre class="lang-cs prettyprint-override"><code>public AutoMapperRegistry()
  {
      var profiles = from t in typeof(AutoMapperRegistry).Assembly.GetTypes()
                      where typeof(Profile).IsAssignableFrom(t)
                      select (Profile)Activator.CreateInstance(t);
      
      var config = new MapperConfiguration(cfg =&gt;
      {
          foreach (var profile in profiles)
          {
              cfg.AddProfile(profile);
          }
      });
  
      For&lt;MapperConfiguration&gt;().Use(config);
      For&lt;IMapper&gt;().Use(ctx =&gt; ctx.GetInstance&lt;MapperConfiguration&gt;().CreateMapper(ctx.GetInstance));
  }
  </code></pre>
  <p>I'm also building a few test cases, implementing MOQ, and this is where i feel a little unsure. whenever calling my controllers, I need to pass in an IMapper like this:</p>
  <pre class="lang-cs prettyprint-override"><code>var mockMapper = new Mock&lt;IMapper&gt;();
  var controller = new MyController(mockMapper.Object);
  </code></pre>
  <p>But then, how do i configure the IMapper to have the correct mappings? It feels redundant to recreate the same logic I've already created before to configure the Mapper. so I am wondering what is the recommended approach to do this?</p>