nhibernate fetchmode.ager를 올바르게 테스트하려면 어떻게해야합니까?
-
06-07-2019 - |
문제
FetchMode.eger가 올바르게 작동하는지 테스트하기 위해 통합 테스트를 작성하는 방법이 있습니까? 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-eger-loading.html
다른 팁
이와 같은 것은 어떻습니까?
MyObject testObject = new MyObject();
testObject.GetEager("a name");
testObject.Session.Close();
Assert.AreEqual(testObject.MySubObject.Id, 3425);
세션을 닫은 다음 객체가 열심히로드되지 않으면 관련 객체에 액세스하려고 시도하면 예외가 발생합니다. 반대로 EAgry로드 된 경우 nhibernate는 데이터베이스에 액세스하려고 시도하지 않으므로 예외를 던지지 않습니다.
제휴하지 않습니다 StackOverflow