Note that there are some explanatory texts on larger screens.

plurals
  1. POUnit Testing Fluent NHibernate. Need help understanding an exception that occurs during a test
    primarykey
    data
    text
    <p>I have the following test support classes.</p> <pre><code> public class FixtureBase { protected SessionSource SessionSource { get; set; } protected ISession Session { get; private set; } [TestFixtureSetUp] public void SetupFixture() { var cfg = Fluently.Configure().Database(SQLiteConfiguration.Standard.ShowSql().InMemory); SessionSource = new SessionSource(cfg.BuildConfiguration().Properties, new TestModel()); } [SetUp] public void SetupContext() { Session = SessionSource.CreateSession(); SessionSource.BuildSchema(Session); } [TearDown] public void TearDownContext() { Session.Close(); Session.Dispose(); } } public TestModel() { this.AddMappingsFromAssembly(typeof(StudentMap).Assembly); } </code></pre> <p>And a very simple test class that is supposed to test the linking class map in a many to many relationship.</p> <pre><code>[TestFixture] public class StudentGuardianAssociationMap_Fixture : FixtureBase { [Test] public void can_correctly_map_studentguardianassociation() { Guardian guardian = new Guardian { FirstName = "GuardianFName", LastName = "GuardianLName", NameSuffix = "GuardianSuffix", MiddleName = "GuardianMiddleName" }; Student student = new Student { FirstName = "StudentFName", LastName = "StudentLName", MiddleName = "StudentMiddleName", Address1 = "StudentAddress1", Address2 = "StudentAddress2", City = "StudentCity", State = "MO", PostalCode = "12345-2342" }; new PersistenceSpecification&lt;StudentGuardianAssociation&gt;(Session) .CheckProperty(x =&gt; x.RelationShipToStudent, 1) .CheckReference(x =&gt; x.AssociatedStudent, student) .CheckReference(x =&gt; x.AssociatedGuardian, guardian) .VerifyTheMappings(); } } </code></pre> <p>When running this test I recieve the following exception.</p> <blockquote> <p>System.ApplicationException : For property 'AssociatedStudent' expected 'Pats.DataTransfer.Student' of type 'Pats.DataTransfer.Student' but got '' of type 'Pats.DataTransfer.Student'</p> </blockquote> <p>Some research shows that this sort of error commonly occurs because when you fail to override object equality in your DTOs, however, I am currently implementing a very rudimentary object equality based on id in my DTO base class.</p> <pre><code> public class DataTranfserObject&lt;TDto&gt; where TDto : DataTranfserObject&lt;TDto&gt; { private int? _oldHashCode; public virtual int Id { get; set; } public virtual int Version { get; set; } public override int GetHashCode() { // Once we have a hash code we'll never change it if (_oldHashCode.HasValue) { return _oldHashCode.Value; } var thisIsTransient = Equals(Id, 0); // When this instance is transient, we use the base GetHashCode() // and remember it, so an instance can NEVER change its hash code. if (thisIsTransient) { _oldHashCode = base.GetHashCode(); return _oldHashCode.Value; } return Id.GetHashCode(); } public override bool Equals(object obj) { var other = obj as TDto; if (other == null) { return false; } // handle the case of comparing two NEW objects var otherIsTransient = Equals(other.Id, 0); var thisIsTransient = Equals(Id, 0); if (otherIsTransient &amp;&amp; thisIsTransient) { return ReferenceEquals(other, this); } return other.Id.Equals(Id); } } </code></pre> <p>And my mapping table's object representation.</p> <pre><code> public class StudentGuardianAssociation : DataTranfserObject&lt;StudentGuardianAssociation&gt; { public virtual int RelationShipToStudent { get; set; } public virtual Student AssociatedStudent { get; set; } public virtual Guardian AssociatedGuardian { get; set; } } </code></pre> <p>And lastly my map for good measure.</p> <pre><code>public StudentGuardianAssociationMap() { LazyLoad(); this.Table("StudentGuardians"); this.Id(x =&gt; x.Id).GeneratedBy.HiLo("100").UnsavedValue(0); this.Version(x =&gt; x.Version).Column("Version"); Map(x =&gt; x.RelationShipToStudent).Column("RelationshipToStudent").Not.Nullable(); References(x =&gt; x.AssociatedGuardian).Not.Nullable(); References(x =&gt; x.AssociatedStudent).Not.Nullable(); } </code></pre> <p>I'm still pretty new to nhibernate and the fluent api, but I have successfully gotten my student and guardian maps to pass their tests. Though I have not yet included the tests for their HasMany Associations portion.</p> <p>Bottom line, what causes the</p> <blockquote> <p>System.ApplicationException : For property 'AssociatedStudent' expected 'Pats.DataTransfer.Student' of type 'Pats.DataTransfer.Student' but got '' of type 'Pats.DataTransfer.Student'</p> </blockquote> <p>exception to be thrown during my test, and what strategy should I take to correct it.</p> <h2>EDIT</h2> <hr> <p>This is the stack trace information returned by nunit.</p> <pre>at FluentNHibernate.Testing.Values.Property`2.CheckValue(Object target) in d:\Builds\FluentNH\src\FluentNHibernate\Testing\Values\Property.cs: line 91 at FluentNHibernate.Testing.PersistenceSpecification`1.c__DisplayClass2.b__1(Property`1 p) in d:\Builds\FluentNH\src\FluentNHibernate\Testing\PersistenceSpecification.cs: line 64 at System.Collections.Generic.List`1.ForEach(Action`1 action) at FluentNHibernate.Testing.PersistenceSpecification`1.VerifyTheMappings(T first) in d:\Builds\FluentNH\src\FluentNHibernate\Testing\PersistenceSpecification.cs: line 64 at FluentNHibernate.Testing.PersistenceSpecification`1.VerifyTheMappings() in d:\Builds\FluentNH\src\FluentNHibernate\Testing\PersistenceSpecification.cs: line 40 at Pats.DataAccessTest.StudentGuardianAssociationMap_Fixture.can_correctly_map_studentguardianassociation() in StudentGuardianAssociationMap_Fixture.cs: line 37 </pre> <p>And the sql executed in case there are any insights there.</p> <pre><code>NHibernate: select next_hi from hibernate_unique_key NHibernate: update hibernate_unique_key set next_hi = @p0 where next_hi = @p1;@p0 = 2, @p1 = 1 NHibernate: INSERT INTO Students (Version, FirstName, LastName, MiddleName, NameSuffix, Address1, Address2, City, State, PostalCode, Id) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10);@p0 = 1, @p1 = 'StudentFName', @p2 = 'StudentLName', @p3 = 'StudentMiddleName', @p4 = NULL, @p5 = 'StudentAddress1', @p6 = 'StudentAddress2', @p7 = 'StudentCity', @p8 = 'MO', @p9 = '12345-2342', @p10 = 1001 NHibernate: select next_hi from hibernate_unique_key NHibernate: update hibernate_unique_key set next_hi = @p0 where next_hi = @p1;@p0 = 3, @p1 = 2 NHibernate: INSERT INTO Guardians (Version, FirstName, LastName, MiddleName, NameSuffix, Id) VALUES (@p0, @p1, @p2, @p3, @p4, @p5);@p0 = 1, @p1 = 'GuardianFName', @p2 = 'GuardianLName', @p3 = 'GuardianMiddleName', @p4 = 'GuardianSuffix', @p5 = 2002 NHibernate: select next_hi from hibernate_unique_key NHibernate: update hibernate_unique_key set next_hi = @p0 where next_hi = @p1;@p0 = 4, @p1 = 3 NHibernate: INSERT INTO StudentGuardians (Version, RelationshipToStudent, Id) VALUES (@p0, @p1, @p2);@p0 = 1, @p1 = 1, @p2 = 3003 NHibernate: SELECT studentgua0_.Id as Id1_2_, studentgua0_.Version as Version1_2_, studentgua0_.RelationshipToStudent as Relation3_1_2_, student1_.Id as Id2_0_, student1_.Version as Version2_0_, student1_.FirstName as FirstName2_0_, student1_.LastName as LastName2_0_, student1_.MiddleName as MiddleName2_0_, student1_.NameSuffix as NameSuffix2_0_, student1_.Address1 as Address7_2_0_, student1_.Address2 as Address8_2_0_, student1_.City as City2_0_, student1_.State as State2_0_, student1_.PostalCode as PostalCode2_0_, guardian2_.Id as Id0_1_, guardian2_.Version as Version0_1_, guardian2_.FirstName as FirstName0_1_, guardian2_.LastName as LastName0_1_, guardian2_.MiddleName as MiddleName0_1_, guardian2_.NameSuffix as NameSuffix0_1_ FROM StudentGuardians studentgua0_ left outer join Students student1_ on studentgua0_.Id=student1_.Id left outer join Guardians guardian2_ on studentgua0_.Id=guardian2_.Id WHERE studentgua0_.Id=@p0;@p0 = 3003 </code></pre> <p>Thanks for wading through my wall of text, I just wanted to be thorough about what I am trying to accomplish, and what I have done thus far.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload