title: Mapper not initialized , error with my unit test Core.Net 2.0
body: <p>I have a WebApi done in Core.net 2.0, with UOW , and automapper.
  Everything is working fine, but now I want to implement Unit Test with Nunit, and I have this error of automapper </p>
  
  <blockquote>
    <p>Message: System.InvalidOperationException : Mapper not initialized.
    Call Initialize with appropriate configuration. If you are trying to
    use mapper instances through a container or otherwise, make sure you
    do not have any calls to the static Mapper.Map methods, and if you're
    using ProjectTo or UseAsDataSource extension methods, make sure you
    pass in the appropriate IConfigurationProvider instance.</p>
  </blockquote>
  
  <p>How can I solve this. Thanks in advance .
  Jolynice</p>
  
  <p>Class AutoMapperProfile.cs</p>
  
  <pre><code>public class AutoMapperProfile : Profile
  {
      public AutoMapperProfile()
      {
          CreateMap&lt;Cars, CarsDTO&gt;()
              .ReverseMap();
      }
  }
  </code></pre>
  
  <p>class Startup.cs</p>
  
  <pre><code>public class Startup
  {
      public IConfiguration Configuration { get; }
  
      public Startup(IConfiguration configuration)
      {
          Configuration = configuration;
      }
      public void ConfigureServices(IServiceCollection services)
      {
  
          //removed configurations 
  
          // Add cors
          services.AddCors();
  
          // Add framework services.
          services.AddMvc();
  
          Mapper.Initialize(cfg =&gt;
          {
              cfg.AddProfile&lt;AutoMapperProfile&gt;();
          });
  
          // Repositories
          services.AddScoped&lt;IUnitOfWork, HttpUnitOfWork&gt;();
          services.AddScoped&lt;IAccountManager, AccountManager&gt;();
      }
  }
  </code></pre>
  
  <p>class carsController.cs</p>
  
  <pre><code>[Authorize]
  [Route("api/[controller]")]
  public class CarsController : Controller
  {
  private IUnitOfWork _unitOfWork;
  readonly ILogger _logger;
  private readonly IAccountManager _accountManager;
  
  public CarsController(
      IUnitOfWork unitOfWork,
      ILogger&lt;CarsController&gt; logger, 
      IAccountManager accountManager)
  {
      _unitOfWork = unitOfWork;
      _logger = logger;
      _accountManager = accountManager;
  }
  
  
  [HttpGet]
  public IActionResult GetAll()
  {
      var allCars = _unitOfWork.CarsRepository.GetAllCarsData();
      if (allCars == null)
      {
          return NotFound();
      }
      return Ok(Mapper.Map&lt;IEnumerable&lt;CarsDTO&gt;&gt;(allCars));
  }
  </code></pre>
  
  <p>and this is my unit test</p>
  
  <pre><code>[TestFixture]
  public class CarsControllerTest
  {
  #region private variables
  List&lt;Cars&gt; cars = new List&lt;Cars&gt;();
  
  CarsController _carsController = null;
  IUnitOfWork _unitOfWork;
  ICarsRepository _carsRepository;
  #endregion
  
  
  [SetUp]
  public void SetUp()
  {
      cars = new List&lt;Cars&gt;
      {
       new Cars
      {
          Alias = "406Moq",
          BrandId = 1,
          ModelId = 1,
          Plate = "00-00-01",
          AltranVehicle = 0,
          DefaultCar = 0,
          Active = 1,
          ColorId = 1
      }
      };
  }
  
  
  [Test]
  public void GetAllCarsControllerTest()
  {
      //Arrange
      _carsRepository = SetupCarsRepository();
      var unityOfWork = new Mock&lt;IUnitOfWork&gt;();
      var _logger = new Mock&lt;ILogger&lt;CarsController&gt;&gt;();
      var accountManager = new Mock&lt;IAccountManager&gt;();
  
      unityOfWork.SetupGet(c =&gt; c.CarsRepository).Returns(_carsRepository);
      _unitOfWork = unityOfWork.Object;
  
      _carsController = new CarsController(_unitOfWork, _logger.Object, accountManager.Object);
  
  
      //Act
      var carsResult = _carsController.GetAll();
  
      //Assert
      carsResult.StatusCode.Should().Be(HttpStatusCode.OK);
  }
  
  private ICarsRepository SetupCarsRepository()
  {
      //initialize  repository
      var mockRepo = new Mock&lt;ICarsRepository&gt;(MockBehavior.Default);
  
      //Setup mocking behavior
      mockRepo.Setup(c =&gt; c.GetAllCarsData()).Returns(cars);
  
      return mockRepo.Object;
  }
  
  //Cleanup
  [TearDown]
  public void TearDown()
  {
      cars = null;
  }
  }
  }
  </code></pre>