How to stop Automapper from matching to a parent class when querying a child class

I am working on implementing AutoMapper in our service, and I see a very confusing problem in our unit tests.

At first this problem includes the following objects and their corresponding maps:

public class DbAccount : ActiveRecordBase<DbAccount> { // this is the ORM entity } public class Account { // this is the primary full valued Dto } public class LazyAccount : Account { // this class as it is named doesn't load the majority of the properties of account } Mapper.CreateMap<DbAccount,Account>(); //There are lots of custom mappings, but I don't believe they are relevant Mapper.CreateMap<DbAccount,LazyAccount>(); //All non matched properties are ignored 

It also includes these objects, although I did not map them to AutoMapper at this point:

 public class DbParty : ActiveRecordBase<DbParty> { public IList<DbPartyAccountRole> PartyAccountRoles { get; set; } public IList<DbAccount> Accounts {get; set;} } public class DbPartyAccountRole : ActiveRecordBase<DbPartyAccountRole> { public DbParty Party { get; set; } public DbAccount Account { get; set; } } 

These classes are converted using custom code, which includes the following, where source is DbParty:

 var party = new Party() //field to field mapping here foreach (var partyAccountRole in source.PartyAccountRoles) { var account = Mapper.Map<LazyAccount>(partyAccountRole.Account); account.Party = party; party.Accounts.Add(account); } 

The test I came across creates a new DbParty, 2 new DbAccounts associated with the new DbParty, and 2 new DbPartyAccountRoles that are associated with the new DbParty and 1 for each of the DbAccounts. He then tests some update features through the DbParty repository. I can include some code for this, if necessary, take a little time to clear.

When it starts by itself, this test works very well, but when launched in the same session as another test (which I will discuss in detail below), calling Mapper in the conversion code above causes this exception:

 System.InvalidCastException : Unable to cast object of type '[Namespace].Account' to type '[Namespace].LazyAccount'. 

Another test also creates a new DbParty, but with only one DbAccount, and then creates 3 DbPartyAccountRoles. I was able to narrow this test down to an exact line that violates another test, and this:

 Assert.That(DbPartyAccountRole.FindAll().Count(), Is.EqualTo(3)) 

Commenting out this line allows you to pass another test.

With this information, I assume the test breaks because something is related to CastleProxy, which is behind the DbAccount object when AutoMapper is called, but I have no idea how.

Now I managed to run the corresponding functional tests (making calls against the service itself), and they seem to be working fine, it makes me think that setting up unit test may be a factor, the most remarkable is that the tests in question are performed in the SqlLite database in the database.

+6
source share
1 answer

The problem ended up being related to running AutoMapper Bootstrapper several times in unit tests; the call was in the TestFixtureSetup method in our base testing class.

The fix was to add the following line before creating the maps:

 Mapper.Reset(); 

I'm still curious to find out why this was the only map that had a problem.

0
source

All Articles