title: Verify that NHibernate has lazy loaded a many-to-one property or not
body: <p>I have a Project and a Person (somewhat simplified):</p>
  
  <pre><code>public class Project
  {
      public virtual Person CreatedBy { get; set; }
  }
  
  public class Person
  {
      public virtual string Username { get; set; }
  }
  </code></pre>
  
  <p>I'm getting a Project with NH Linq:</p>
  
  <pre><code>var projects = from p in session.Query&lt;Project&gt;()
               select p;
  var project = projects.First();
  </code></pre>
  
  <p>and sometimes with Fetch()</p>
  
  <pre><code>var project = projects.Fetch(p =&gt; p.CreatedBy).First();
  </code></pre>
  
  <p>Now, how can I verify in a unit test that project.CreatedBy <strong>is not</strong> or <strong>is</strong> eagerly loaded (and the reverse), depending on if Fetch() was used? In my understanding, using Fetch() should eager load the Person and if I don't use Fetch() it should lazy load the Person.</p>
  
  <p>I've tried </p>
  
  <pre><code>Assert.IsTrue(NHibernateUtil.IsInitialized(project.CreatedBy));
  </code></pre>
  
  <p>And </p>
  
  <pre><code>NHibernateUtil.IsPropertyInitialized(project, "CreatedBy")
  </code></pre>
  
  <p>But they both return true whether I use Fetch() or not. IsInitialized() works fine with collections (one-to-many), for instance Person.Projects, but not with many-to-one...</p>
  
  <p>The CreatedBy property should be lazy loaded but sometimes I want it to be eagerly loaded and I want unit tests confirming that it's working as intended.</p>
  
  <p>The mapping for the CreatedBy property is</p>
  
  <pre><code>&lt;many-to-one name="CreatedBy" class="Person" fetch="select"&gt;
    &lt;column name="PERSON_ID" precision="10" scale="0" not-null="true" /&gt;
  &lt;/many-to-one&gt;
  </code></pre>
  
  <p>I verified the mapping like this:</p>
  
  <pre><code>var to1 = NHibernateConfiguration.GetClassMapping(typeof(Project))
      .ReferenceablePropertyIterator.FirstOrDefault(p =&gt; p.Name == "CreatedBy")
      .Value as NHibernate.Mapping.ToOne;
  
  Assert.IsTrue(to1.IsLazy);
  Assert.IsFalse(to1.UnwrapProxy);
  </code></pre>
  
  <p>Which passes.</p>
  
  <p>Any ideas?</p>