title: How to mock Automapper (IMapper) in controller
body: <p>I am trying to write a unit test for my existing MVC Web Aplication. In that I am facing some problem in automapper (<code>IMapper</code>) Whenever am using map function it returns <code>null</code> value.</p>
  
  <p>My Controller Code:</p>
  
  <pre><code>public class UserAdministrationController : BaseController
  {
      private readonly iUserService _userService;
      private readonly IMapper _mapper;
  
      public NewsController(iUserService userService, IMapper mapper)
      {
          _userService = userService;
          _mapper = mapper;
      }
  
      public ActionResult Create(int CompanyID == 0)
      {            
          UserDetail data = _userService(CompanyID);
          var Modeldata = _mapper.Map&lt;UserDetailViewModel, UserDetail&gt;(data);
          return View(Modeldata);
      }
  } 
  </code></pre>
  
  <p>Mock Mapping Code:</p>
  
  <pre><code>public class MappingDataTest : CommonTestData
  {
      public Mock&lt;IMapper&gt; MappingData()
      {
          var mappingService = new Mock&lt;IMapper&gt;();
          UserDetailViewModel interview = getUserDetailViewModel(); // get value of UserDetailViewModel
          UserDetail im = getUserDetail(); // get value of UserDetails
  
          mappingService.Setup(m =&gt; m.Map&lt;UserDetail, UserDetailViewModel&gt;(im)).Returns(interview);
          mappingService.Setup(m =&gt; m.Map&lt;UserDetailViewModel, UserDetail&gt;(interview)).Returns(im);
  
          return mappingService;
      }
  }
  </code></pre>
  
  <p>Mocking Code:</p>
  
  <pre><code>[TestClass]
  public class UserAdminControllerTest
  {
      private MappingDataTest _common;
  
      [TestInitialize]
      public void TestCommonData()
      {
          _common = new MappingDataTest();
      }
  
      [TestMethod]
      public void UserCreate()
      {
          //Arrange                                               
          UserAdministrationController controller = new UserAdministrationController(_common.mockUserService().Object, _common.MappingData().Object);
          controller.ControllerContext = _common.GetUserIdentity(controller);
  
          // Act
          ViewResult newResult = controller.Create() as ViewResult;
  
          // Assert
          Assert.IsNotNull(newResult);
      }
  }
  </code></pre>
  
  <p>Mapper is not working its always showing the <code>null</code> value in controller. kindly help me. Thanks in Advance.</p>