Greg
Greg

Reputation: 2690

Automapper Failing Unit Test

I have just decided that I would try out 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

Upvotes: 2

Views: 2883

Answers (1)

Claudio Redi
Claudio Redi

Reputation: 68400

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.

Upvotes: 7

Related Questions