Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
pvredeveld opened this issue Aug 2, 2024 · 0 comments

Comments

@pvredeveld
Copy link

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; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant