【发布时间】:2010-01-21 16:47:59
【问题描述】:
我有一个使用 NHibernate 会话将对象持久保存到数据库的存储库类。默认情况下,存储库不使用显式事务 - 这取决于调用者来管理。我有以下单元测试来测试我的 NHibernate 管道:
[Test]
public void NHibernate_BaseRepositoryProvidesRequiredMethods()
{
using (var unitOfWork = UnitOfWork.Create())
{
// test the add method
TestRepo.Add(new TestObject() { Id = 1, Name = "Testerson" });
TestRepo.Add(new TestObject() { Id = 2, Name = "Testerson2" });
TestRepo.Add(new TestObject() { Id = 3, Name = "Testerson3" });
// test the getall method
var objects = TestRepo.GetAll();
Assert.AreEqual(3, objects.Length);
// test the remove method
TestRepo.Remove(objects[1]);
objects = TestRepo.GetAll();
Assert.AreEqual(2, objects.Length);
// test the get method
var obj = TestRepo.Get(objects[1].Id);
Assert.AreSame(objects[1], obj);
}
}
问题在于这条线
Assert.AreEqual(3, objects.Length);
测试失败,因为GetAll 方法返回的对象列表为空。如果我在插入三个对象后立即手动刷新会话,则该部分测试通过。我在会话上使用默认的 FlushMode,根据文档,它应该在运行查询以检索所有对象之前刷新,但显然不是。我错过了什么?
编辑:我将 Sqlite 用于单元测试场景,如果这有什么不同的话。
【问题讨论】:
标签: nhibernate