我刚决定尝试使用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
答案 0 :(得分:7)
问题是您在测试中配置映射并期望另一个测试使用此配置。
考虑到它们应该是独立的,因此一个测试不能依赖于其他测试执行。这非常重要
您需要在Mappings_ViewModel_Address_ToDTO_pass
或常用设置上创建映射。