mardi 29 septembre 2015

Assigning IMappingEngine in constructor causes mapping exception when mapping only when running from unit test

I have an unit test - where I do AutoMapperConfiguration in setup. I then set IMappingEngine as private property in the constructor in my class where I actually do mapping. Unit test fails if I use this property, but using the static method from automapper works fine. Both methods work fine when running the actual program. Only difference I can see is the unit tests are in a separate assembly. CLS compliance is turned on.

public class AutomapperConfiguration
{
    public static void Configure()
    {
        Mapper.Initialize(cfg =>
        {
            cfg.AddProfile<AclassMappingProfile>();
        });
    }
    public static void Reset()
    {
        Mapper.Reset();
    }
}
public class AssetModelFactoryTests
{
    [SetUp]
    public void SetUp()
    {
        AutomapperConfiguration.Configure();
    }
    [Test]
    public void TestA()
    {
        var a = new A();
    }
}

public class A
{
    private IMappingEngine _mappingEngine;
    public A()
    {
         _mappingEngine = Mapper.Engine;
    }

    public void DoA()
    {
         Mapper.Map<Destination>(source); //works
         _mappingEngine.Map<Destionation>(source); //Throws mapping not supported
    }
}

Aucun commentaire:

Enregistrer un commentaire