我刚刚决定尝试 Automapper。
我在我的项目中编写了一些单元测试,当我“全部运行”时,它们都按预期通过了。但是,当我运行单个测试时,它们会失败......那么,我是否缺少一些特殊设置?
这是代码。当我全部运行时,这通过了测试。
[TestMethod]
public void Mappings_ConfigureMappings_pass()
{
Mapper.CreateMap<Address, AddressDTO>();
Mapper.AssertConfigurationIsValid();
}
但是当我运行实际的映射测试时,测试失败了。
[TestMethod]
public void Mappings_ViewModel_Address_ToDTO_pass()
{
var address = new Address()
{
Address1 = "Line1",
Address2 = "Line2",
Address3 = "Line3",
Country = "ROI",
County = "Co Dublin",
PostCode = "ABC",
TownOrCity = "Dublin"
};
AddressDTO addressDTO = Mapper.Map<Address, AddressDTO>(address);
Assert.IsNotNull(addressDTO);
Assert.AreEqual("ROI", addressDTO.Country);
Assert.AreEqual("Dublin", addressDTO.TownOrCity);
}
这里是相关的类......你可以看到它们是相同的,那么为什么测试失败了?这是我在设置中错过的东西吗?
public class Address
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public string County { get; set; }
public string Country { get; set; }
public string PostCode { get; set; }
public string TownOrCity { get; set; }
}
public class AddressDTO
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public string County { get; set; }
public string Country { get; set; }
public string PostCode { get; set; }
public string TownOrCity { get; set; }
}
这是失败消息:
Missing type map configuration or unsupported mapping.
Mapping types:
Address -> AddressDTO
UI.Address -> Services.DataTransferObjects.AddressDTO
Destination path:
AddressDTO
Source value:
UI.Address