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> {
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()
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.