2

我刚刚决定尝试 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
4

1 回答 1

7

问题是您正在测试中配置映射并期望另一个测试使用此配置。

考虑到它们应该是独立的,因此一个测试不能依赖于其他测试执行。这真的很重要

您需要Mappings_ViewModel_Address_ToDTO_pass在通用设置上创建映射。

于 2013-06-13T21:42:44.827 回答