从挖掘文章和文档,我的猜测是AutoMapper需要对只读类型进行特殊编码.这会是什么样的?
注意:我尝试了Mapper.AssertConfigurationIsValid();正如另一篇文章所建议,但没有任何改进
Test Name: CreateOrder_ValidContract_CreatesNewOrder Test
FullName: ACME.Maintenance.Domain.Test.OrderServiceTest.CreateOrder_ValidContract_CreatesNewOrder
Test Source: C:\Users\me\documents\visual studio
2015\Projects\ACME.Maintenance\ACME.Maintenance.Domain.Test\OrderServiceTest.cs
: line 65 Test Outcome: Failed Test Duration: 0:00:00.0233941Result StackTrace: at lambda_method(Closure,ContractDto,Contract
,ResolutionContext ) at
ACME.Maintenance.Domain.ContractService.GetById(String contractId) in
C:\Users\me\documents\visual studio
2015\Projects\ACME.Maintenance\ACME.Maintenance.Domain\ContractService.cs:line
34 at
ACME.Maintenance.Domain.Test.OrderServiceTest.CreateOrder_ValidContract_CreatesNewOrder()
in C:\Users\me\documents\visual studio
2015\Projects\ACME.Maintenance\ACME.Maintenance.Domain.Test\OrderServiceTest.cs:line
69 Result Message: Test method
ACME.Maintenance.Domain.Test.OrderServiceTest.CreateOrder_ValidContract_CreatesNewOrder
threw exception: AutoMapper.AutoMapperMappingException: Missing type
map configuration or unsupported mapping.Mapping types: ContractDto -> Contract
ACME.Maintenance.Domain.DTO.ContractDto -> ACME.Maintenance.Domain.Contract
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>)); }
ContractService类:
public Contract GetById(string contractId) { var contractDto = _contractRepository.GetById(contractId); var contract = Mapper.Map<ContractDto,Contract>(contractDto); Mapper.AssertConfigurationIsValid(); return contract; }
合同类:
public class Contract { public string ContractId { get; set; } public DateTime ExpirationDate { get; set; } }
ContractDto类:
public class ContractDto { public string ContractId { get; set; } public DateTime ExpirationDate { get; set; } }