有没有办法编写集成测试来测试FetchMode.Eager是否正常工作?我想验证当我检索MySubObject时它不会进入数据库。

代码:

public MyObject GetEager(string name)
{
    return Session
        .CreateCriteria(typeof(MyObject))
        .SetFetchMode("MySubObject", FetchMode.Eager)
        .Add(Restrictions.Eq("Name", name))
        .UniqueResult<MyObject>();
}
有帮助吗?

解决方案

您也可以使用 NHibernateUtil.IsInitialized ...,如本文所述

http://nhibernate.info/doc/howto /various/lazy-loading-eager-loading.html

其他提示

这样的事情怎么样;

MyObject testObject = new MyObject();
testObject.GetEager("a name");
testObject.Session.Close();
Assert.AreEqual(testObject.MySubObject.Id, 3425);

通过关闭会话然后尝试访问关联的对象,如果对象没有被急切地加载,它将抛出异常。相反,如果它被轻微加载,NHibernate将不会尝试访问数据库,因此不会抛出异常。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top