When I implemented IReadOnlyList
in my code, my unit test threw an AutoMapperMappingException
.
Missing type map configuration or unsupported mapping.
From digging through articles and documentation, my guess is that AutoMapper needs special coding for readonly
types. What would this look like?
Note: I tried Mapper.AssertConfigurationIsValid();
as another post suggested, but no improvements.
Mapper.Initialize(cfg => cfg.CreateMap<ContractDto, Contract>());
Mapper.Initialize(cfg => cfg.CreateMap<PartDto, Part>());
[TestMethod]
public void CreateOrder_ValidContract_CreatesNewOrder()
{
//Arrange
var orderService = new OrderService();
var contractService = new ContractService(_contractRepository);
var contract = contractService.GetById(ValidContractId);
// Act
var newOrder = orderService.CreateOrder(contract);
// Assert
Assert.IsInstanceOfType(newOrder, typeof(Order));
Guid guidOut;
Assert.IsTrue(Guid.TryParse(newOrder.OrderId, out guidOut));
Assert.AreEqual(newOrder.Status, ContractStatus.New);
Assert.IsInstanceOfType(newOrder.Items, typeof(IReadOnlyList<OrderItem>));
}
Domain Layer: ContractService class
public Contract GetById(string contractId)
{
var contractDto = _contractRepository.GetById(contractId);
var contract = Mapper.Map<ContractDto, Contract>(contractDto);
Mapper.AssertConfigurationIsValid();
return contract;
}
StackTrace:
lambda_method(Closure , ContractDto , Contract , ResolutionContext )
at ACME.Maintenance.Domain.ContractService.GetById(String contractId) at OrderServiceTest.CreateOrder.Order_ValidContract_CreatesNewOrder()
Aucun commentaire:
Enregistrer un commentaire