Is it possible to use PersistenceSpecification and CheckList for unit testing an Inverse HasMany relationship?
This is actually my very first question on Stackoverflow. I hope its not the las开发者_开发知识库t and hopefully I will contribute with some answers myself.
My question is simple. I'm stuck with a NUnit test where I'm testing a inversed HasMany relationship. Everytime I debug it, the output is "Actual count does not equal expected count...". I've done some extensive research on the net about this issue and some people claim that it's impossible to do such a unittest.
Here is my example. Code for EmployeeMapping:
Id(x => x.ID).GeneratedBy.Identity();
HasMany(x => x.PositionHistory).KeyColumn("Employee_id").Inverse().Cascade.All();
Here is code for EmployeePositionMapping:
Id(x => x.ID).GeneratedBy.Identity();
References(x => x.EmployeeAtPosition).Cascade.All().Nullable();
The Unit test is like this:
var position2 = new EmployeePosition();
position2.ExtraBenefit = 353.00;
position2.Salary = 23300.00;
position2.Region = region;
position2.Department = department;
position2.Year = "2010";
position2.Comment = "kommentar igen...";
position2.History = true;
var positionList = new List<EmployeePosition>();
positionList.Add(position2);
new PersistenceSpecification<Employee>(Session, new CustomEqualityComparer<Employee, long>())
.CheckProperty(p => p.ID, Int64.Parse("1"))
.CheckList(p => p.PositionHistory, positionList, (emp, empPosition) => emp.AddEmployeePosition(empPosition))
.VerifyTheMappings();
I would like to replace the Nullable() attribute to Not.Nullable() for References EmployeeAtPosition property.
I have a Entity base class that take cares of comparing entities and even Transients. The AddEmployeePosition() takes care of maintaining the relationship both ways for Employee.PositionHistory and EmployeePosition.EmployeeAtPosition.
When I look at the Nhibernate SQL in output It seems ok. But for some reason it's not equal the list I've prepared (positionList)
Other useful facts: I'm running Nhibernate 3.0 and the matching fluent Nhibernate binaries. Downloded them for three days ago. Using SQLite InMemory for conducting Mapping Tests.
I really appreciate all good new angles that your hints may give me...
/Best Regards BacceSR- from a very snowy Stockholm.
Hmm no Answers. Better answer this one myself :D. Well I found the answer after some googling, and there is currently some issues with CheckList method. I used CheckComponent instead and it worked fine. Now "Parent" object is inserted before its "Children" and therefore no error. CheckList tried to do the opposite which didn't work at all...
I also got this issue this night and just spent about two hours with this very strange error message. Wasted much time for checking every line of code before googling to know this is not my fault heheheh :D.
According to this post, we simply can't use it with inverse=true. Unfortunately, CheckComponentList didn't work for me, I got the same error.
Also found a dead simple explanation about inverse keyword, IMHO, is really a bad term.
精彩评论