title: How to return null when accessing a moq object?
body: <p>I am using Moq library for unit testing. Now what i want is that when I access my object for the first time it should return null, and when i access this on second time it should return something else.</p>
  
  <p>here is my code</p>
  
  <pre><code>var mock = new Mock&lt;IMyClass&gt;();
  mock.Setup(?????);
  mock.Setup(?????);
  
  var actual = target.Method(mock.object);
  </code></pre>
  
  <p>in my method i am first checking that whether mock object is null or not, if it is null then do initialize it and then do some calls on it.</p>
  
  <pre><code>bool Method(IMyClass myObj)
  {
      if (myObj != null)
          return true;
      else
      {
          myObj = new MyClass();
          bool result = myObj.SomeFunctionReturningBool();
          return result;
      }
  }
  </code></pre>
  
  <p>what to do setup for mock object,</p>
  
  <p>Also i need to know how to mock this line</p>
  
  <pre><code>bool result = myObj.SomeFunctionReturningBool();
  </code></pre>