Come testare correttamente NHibernate FetchMode.Eager?
-
06-07-2019 - |
Domanda
Esiste un modo per scrivere un test di integrazione per verificare che FetchMode.Eager funzioni correttamente? Voglio verificare che non vada nel database quando recupero MySubObject.
Il codice:
public MyObject GetEager(string name)
{
return Session
.CreateCriteria(typeof(MyObject))
.SetFetchMode("MySubObject", FetchMode.Eager)
.Add(Restrictions.Eq("Name", name))
.UniqueResult<MyObject>();
}
Soluzione
Puoi anche usare NHibernateUtil.IsInitialized
... come spiegato in questo post
http://nhibernate.info/doc/howto /various/lazy-loading-eager-loading.html
Altri suggerimenti
Che ne dici di qualcosa del genere;
MyObject testObject = new MyObject();
testObject.GetEager("a name");
testObject.Session.Close();
Assert.AreEqual(testObject.MySubObject.Id, 3425);
Chiudendo la sessione e quindi tentando di accedere all'oggetto associato se l'oggetto non viene caricato avidamente, verrà generata un'eccezione. Al contrario, se è caricato con entusiasmo, NHibernate non tenterà di accedere al database e quindi non genererà l'eccezione.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow