title: Automapper in xUnit testing and .NET Core 2.0
body: <p>I have .NET Core 2.0 Project which contains Repository pattern and xUnit testing.</p>
  
  <p>Now, here is some of it's code.</p>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>public class SchedulesController : Controller
  {
      private readonly IScheduleRepository repository;
      private readonly IMapper mapper;
  
      public SchedulesController(IScheduleRepository repository, IMapper mapper)
      {
          this.repository = repository;
          this.mapper = mapper;
      }
  
      [HttpGet]
      public IActionResult Get()
      {
          var result = mapper.Map&lt;IEnumerable&lt;Schedule&gt;, IEnumerable&lt;ScheduleDto&gt;&gt;(source: repository.items);
          return new OkObjectResult(result);
      }
  }
  </code></pre>
  
  <p><strong>My Test Class:</strong></p>
  
  <pre><code>public class SchedulesControllerTests
  {
      [Fact]
      public void CanGet()
      {
          try
          {
              //Arrange
              Mock&lt;IScheduleRepository&gt; mockRepo = new Mock&lt;IScheduleRepository&gt;();
              mockRepo.Setup(m =&gt; m.items).Returns(new Schedule[]
              {
                  new Schedule() { Id=1, Title = "Schedule1" },
                  new Schedule() { Id=2, Title = "Schedule2" },
                  new Schedule() { Id=3, Title = "Schedule3" }
              });
  
              var mockMapper = new Mock&lt;IMapper&gt;();
              mockMapper.Setup(x =&gt; x.Map&lt;Schedule&gt;(It.IsAny&lt;ScheduleDto&gt;()))
                  .Returns((ScheduleDto source) =&gt; new Schedule() { Title = source.Title });
  
              SchedulesController controller = new SchedulesController(repository: mockRepo.Object, mapper: mockMapper.Object);
  
              //Act
              var result = controller.Get();
  
              //Assert
              var okResult = result as OkObjectResult;
              Assert.NotNull(okResult);
  
              var model = okResult.Value as IEnumerable&lt;ScheduleDto&gt;;
              Assert.NotNull(model);
  
          }
          catch (Exception ex)
          {
              //Assert
              Assert.False(false, ex.Message);
          }
      }
  }
  </code></pre>
  
  <p><strong>Issue I Am facing.</strong></p>
  
  <p>My Issue is that when I run this code with database context and execute <code>Get()</code> method, it works fine, it gives me all results.</p>
  
  <p>But when I tries to run test case, it's not returning any data of Dto object.
  When I debugged I found that</p>
  
  <ol>
  <li><p>I am getting my test object in controller using <code>mockRepo</code>.</p></li>
  <li><p>But it looks like Auto mapper is not initialized correctly, because while mapping it's not returning anything in</p>
  
  <p><code>var result = mapper.Map&lt;IEnumerable&lt;Schedule&gt;, IEnumerable&lt;ScheduleDto&gt;&gt;(source: repository.items);</code></p></li>
  </ol>
  
  <p><strong>What I tried So Far?</strong></p>
  
  <p>I followed all this answers but still it's not working.</p>
  
  <p><a href="https://stackoverflow.com/questions/22984530/mocking-mapper-map-in-unit-testing">Mocking Mapper.Map() in Unit Testing</a></p>
  
  <p><a href="https://stackoverflow.com/questions/29605470/how-to-mock-a-list-transformation-using-automapper">How to Mock a list transformation using AutoMapper</a></p>
  
  <p>So, I need help from someone who is good in xUnit and automapper, and need guidance on how to initialize mock Mapper correctly.</p>