gpt4 book ai didi

c# - 映射器未初始化,我的单元测试 Core.Net 2.0 出错

转载 作者:太空狗 更新时间:2023-10-29 20:49:09 24 4
gpt4 key购买 nike

我有一个在 Core.net 2.0 中完成的 WebApi,带有 UOW 和自动映射器。一切正常,但现在我想用 Nunit 实现单元测试,但出现自动映射器错误

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.

我该如何解决这个问题。提前致谢 。乔丽妮丝

类 AutoMapperProfile.cs

public class AutoMapperProfile : Profile
{
public AutoMapperProfile()
{
CreateMap<Cars, CarsDTO>()
.ReverseMap();
}
}

Startup.cs 类

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 =>
{
cfg.AddProfile<AutoMapperProfile>();
});

// Repositories
services.AddScoped<IUnitOfWork, HttpUnitOfWork>();
services.AddScoped<IAccountManager, AccountManager>();
}
}

carsController.cs 类

[Authorize]
[Route("api/[controller]")]
public class CarsController : Controller
{
private IUnitOfWork _unitOfWork;
readonly ILogger _logger;
private readonly IAccountManager _accountManager;

public CarsController(
IUnitOfWork unitOfWork,
ILogger<CarsController> 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<IEnumerable<CarsDTO>>(allCars));
}

这是我的单元测试

[TestFixture]
public class CarsControllerTest
{
#region private variables
List<Cars> cars = new List<Cars>();

CarsController _carsController = null;
IUnitOfWork _unitOfWork;
ICarsRepository _carsRepository;
#endregion


[SetUp]
public void SetUp()
{
cars = new List<Cars>
{
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<IUnitOfWork>();
var _logger = new Mock<ILogger<CarsController>>();
var accountManager = new Mock<IAccountManager>();

unityOfWork.SetupGet(c => 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<ICarsRepository>(MockBehavior.Default);

//Setup mocking behavior
mockRepo.Setup(c => c.GetAllCarsData()).Returns(cars);

return mockRepo.Object;
}

//Cleanup
[TearDown]
public void TearDown()
{
cars = null;
}
}
}

最佳答案

您在单元测试中缺少映射器的初始化。以下代码在 CarsControllerTest 类构造函数中初始化映射器。

[TestFixture]
public class CarsControllerTest
{
public CarsControllerTest()
{
Mapper.Initialize(cfg =>
{
cfg.AddProfile<AutoMapperProfile>();
});
}
}

关于c# - 映射器未初始化,我的单元测试 Core.Net 2.0 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47481934/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com