title: Mocking complex object - testing with Moq
body: <p>I have following Object structure:</p>
  
  <pre><code>public class A
  {
      //...
      public B b;
      //...
  }
  
  public class B
  {
      //...
      public C c;
      //...
  }
  
  public class C
  {
      //...
      //...
  }
  </code></pre>
  
  <p>I want to create some tests to written database access code. To achieve this, before each test, I will add do database some neccessary data (I am using fluent nHibernate).</p>
  
  <p>So to test some database method performing some actions on entity A, I need to save object of class A to database</p>
  
  <pre><code>//...
  var a = new A();
  session.Save(a);
  //...
  </code></pre>
  
  <p>Which leads to nullreference exception, because objects of classes B and C are also database entities that can't be nullable. </p>
  
  <p>My question is how to avoid such exception in elegant way, by using Moq. In my real problem the object trees are far more complex than in this simplified question. </p>