简体   繁体   English

自动化失败单元测试

[英]Automapper Failing Unit Test

I have just decided that I would try out Automapper. 我刚刚决定尝试使用Automapper。

I wrote a few unit tests in my project and when I "Run All", they all pass as expected. 我在我的项目中写了几个单元测试,当我“全部运行”时,它们都按预期通过。 However, when I run individual tests, they fail... so, is there some special setup I am missing? 但是,当我运行单独的测试时,它们会失败...所以,是否有一些特殊的设置我错过了?

Here is the code. 这是代码。 This passes the test when I Run All. 当我全部运行时,这通过了测试。

 [TestMethod]
    public void Mappings_ConfigureMappings_pass()
    {           
        Mapper.CreateMap<Address, AddressDTO>();
        Mapper.AssertConfigurationIsValid();
    }

but when I run an actual mapping test , the test fails. 但是当我运行实际的映射测试时,测试失败。

[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);
    }

and here are the relevant classes...as you can see they are identical, so why is the test failing? 这是相关的类...如您所见,它们是相同的,那么为什么测试失败? Is it something I have missed in the setup? 这是我在设置中遗漏的东西吗?

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; } 
}

Here is the failure message: 这是失败消息:

Missing type map configuration or unsupported mapping.

Mapping types:
Address -> AddressDTO
UI.Address -> Services.DataTransferObjects.AddressDTO

Destination path:
AddressDTO

Source value:
UI.Address

The problem is that you're configuring the mapping inside a test and expecting another test to use this configuration. 问题在于您正在测试中配置映射,并期望另一个测试使用此配置。

Take into account they are supposed to be independent so one test can't rely on other test execution. 考虑到它们应该是独立的,因此一个测试不能依赖其他测试执行。 This is really important 这非常重要

You'll need to create the mapping on Mappings_ViewModel_Address_ToDTO_pass or on a common setup. 您需要在Mappings_ViewModel_Address_ToDTO_pass或常用设置上创建映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM