Skip to content

Isolate Mapper configurations in unit tests question / unexpected behavior using Mapper.ResetDefaultInstance #238

@pvredeveld

Description

@pvredeveld

In a testing scenario I run into the problem the mapper configuration cannot be reset between unit tests using the static API.

In my scenario some of the unit tests share the same mapper configuration. Both explicit and implicit mappings. See code below.
When I understand the documentation correctly, Mapper.ResetDefaultInstance should be used, to accomplice a configuration reset.
But it throws an InvalidOperationException with the message: variable 'peToPmData' of type 'AgileObjects.AgileMapper.ObjectPopulation.IObjectMappingData`2[ProductEntity,ProductModel]' referenced from scope '', but it is not defined, at the last statement of TestTwo.

Without the call to Mapper.ResetDefaultInstance there is a (understandable) a MappingConfigurationException on the explicit mapping statement in the setup .

How should I solve this? Moving the setup away to a (xunit) fixture (as the original code has) , will not solve my problem, because different tests in the same project need different fixtures, but the same mappings.

void Main()
{
	TestOne();
	TestTwo();
}

void SetUp()
{
	Mapper.ResetDefaultInstance();
	Mapper.WhenMapping.From<OtherEntity>().ToANew<OtherModel>().Ignore(d => d.Name);
}

void TestOne()
{
	SetUp(); // some code requires specific mapping
	var pe = new ProductEntity();
	var pm = Mapper.Map(pe).ToANew<ProductModel>();
}

void TestTwo()
{
	SetUp(); // some code requires specific mapping
	var pe = new ProductEntity();
	var pm = Mapper.Map(pe).ToANew<ProductModel>();
}


public class ProductModel 
{
	public long Id { get; private set; }
	public string Name { get; private set; }

	public ProductModel(long id, string name)
	{
		Id = id;
		Name = name;
	}
}

public class ProductEntity
{
	public long Id { get; set; }
	public string Name { get; set; }
}

public class OtherModel
{
	public long Id { get; set; }
	public IReadOnlyList<char> Name { get; set; }
}

public class OtherEntity
{
	public long Id { get; set; }
	public IList<char> Name { get; set; }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions