级联测试“级联什么? “

在应用程序中, 当你对子对象进行了一些改变,你肯定不希望需要自己去想着还需要对父对象进行哪些操作,这样是非常繁琐的。好在 NHibernate 提供了级联的功能,你不需要做这些事了。如果你的映射是正确的,你就只需要保存子对象,其他的工作你都不需要理会。

对于一些人,尤其像我这样的,这是一个很大的工作量,这就是为什么我们要测试映射。

[Test()] public void CanCascadeSaveFromCourseToSections() { using (SQLiteDatabaseScope<CourseMapping> Scope = new SQLiteDatabaseScope<CourseMapping>()) { using (ISession Session = Scope.OpenSession()) { Guid ID; Term Term = new Term { Name = "Fall 2009", StartDate = new System.DateTime(2009, 9, 1), EndDate = new System.DateTime(2009, 12, 1) }; //We're not testing the cascade of section -> term here using (ITransaction Tran = Session.BeginTransaction()) { Session.Save(Term); Tran.Commit(); } Session.Clear(); Course Course = new Course { Subject = "SUBJ", CourseNumber = "1234", Title = "Title", Description = "Description", Hours = 3 }; Section Section1 = new Section { FacultyName = "FacultyName", RoomNumber = "R1", SectionNumber = "1", Term = Term }; Section Section2 = new Section { FacultyName = "FacultyName", RoomNumber = "R1", SectionNumber = "2", Term = Term }; Course.AddSection(Section1); Course.AddSection(Section2); //Test saving using (ITransaction Tran = Session.BeginTransaction()) { ID = (Guid) Session.Save(Course); Tran.Commit(); } Session.Clear(); //Check the results using (ITransaction Tran = Session.BeginTransaction()) { Course = Session.Get<Course>(ID); Assert.AreEqual(2, Course.Sections.Count); Assert.AreEqual(1, Course.Sections .Where(S => S.Equals(Section1)).Count(), "Course.Sections does not contain section 1."); Assert.AreEqual(1, Course.Sections .Where(S => S.Equals(Section2)).Count(), "Course.Sections does not contain section 2."); Tran.Commit(); } } } }

上面的测试将确保档你保存 course 的时候,也同样会保存对 section 的操作。下面看看它是如何工作的:

  • 获取一个新的 SQLite 数据库
  • 虽然我们不会测试 term ,但是因为 section 的需要,所以我们也需要在数据库中建立
  • 创建一个 course 和两个 section
  • 保存 course
  • 清空 session
  • 获取到 course
  • 确保它有两个 section

当你从 course 中移除一个 section 将会发生什么呢?当然,任何一个 section 都需要有一个父对象,也就是 course 。请记住,我们在映射里指定的是非空的。更重要的是,现实世界中不会有独立的 section 。所以,当 section 成为孤儿的时候,它将被删除掉,下面我们来进行这样的一个测试:

[Test()] public void CanCascadeOrphanDeleteFromCourseToSections() { using (SQLiteDatabaseScope<CourseMapping> Scope = new SQLiteDatabaseScope<CourseMapping>()) { using (ISession Session = Scope.OpenSession()) { Guid ID; Term Term = new Term { Name = "Fall 2009", StartDate = new System.DateTime(2009, 9, 1), EndDate = new System.DateTime(2009, 12, 1) }; using (ITransaction Tran = Session.BeginTransaction()) { //We're not testing the cascade of section -> term here Session.Save(Term); Tran.Commit(); } Session.Clear(); Course Course = new Course { Subject = "SUBJ", CourseNumber = "1234", Title = "Title", Description = "Description", Hours = 3 }; Section Section1 = new Section { FacultyName = "FacultyName", RoomNumber = "R1", SectionNumber = "1", Term = Term }; Section Section2 = new Section { FacultyName = "FacultyName", RoomNumber = "R1", SectionNumber = "2", Term = Term }; Course.AddSection(Section1); Course.AddSection(Section2); using (ITransaction Tran = Session.BeginTransaction()) { Session.Save(Course); Tran.Commit(); } Session.Clear(); //Test removing Course.RemoveSection(Section1); using (ITransaction Tran = Session.BeginTransaction()) { ID = (Guid) Session.Save(Course); Tran.Commit(); } Session.Clear(); //Check the results using (ITransaction Tran = Session.BeginTransaction()) { Course = Session.Get<Course>(ID); Assert.AreEqual(1, Course.Sections.Count()); Assert.AreEqual(0, Course.Sections .Where(S => S.Equals(Section1)).Count(), "Course.Sections still contains section 1"); Tran.Commit(); } } } }

我希望大家能和我是一样的心态,除了查询测试,当我们写 DAO 层的时候,就要对 NHibernate 进行测试。我们对其他的实体类也都要进行相同类型的测试。

但是…

那么,你肯定在想“这么做太混乱了,肯定无法编译,就算可以,差不多所有的测试都会失败!”是的,如果