我刚决定尝试Automapper.
我在我的项目中写了几个单元测试,当我"全部运行"时,它们都按预期通过.但是,当我运行单独的测试时,它们会失败...所以,是否有一些特殊的设置我错过了?
这是代码.当我全部运行时,这通过了测试.
[TestMethod]
public void Mappings_ConfigureMappings_pass()
{
Mapper.CreateMap<Address, AddressDTO>();
Mapper.AssertConfigurationIsValid();
}
Run Code Online (Sandbox Code Playgroud)
但是当我运行实际的映射测试时,测试失败了.
[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);
}
Run Code Online (Sandbox Code Playgroud)
这里是相关的类......你可以看到它们是相同的,为什么测试失败?这是我在设置中遗漏的东西吗?
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; }
}
Run Code Online (Sandbox Code Playgroud)
这是失败消息:
Missing type map configuration or unsupported mapping.
Mapping types:
Address -> AddressDTO
UI.Address -> Services.DataTransferObjects.AddressDTO
Destination path:
AddressDTO
Source value:
UI.Address
Run Code Online (Sandbox Code Playgroud)
问题是您正在测试中配置映射并期望另一个测试使用此配置.
考虑到它们应该是独立的,因此一个测试不能依赖于其他测试执行.这非常重要
您需要Mappings_ViewModel_Address_ToDTO_pass在常用设置上或上创建映射.
| 归档时间: |
|
| 查看次数: |
1828 次 |
| 最近记录: |