title: Moq Using Lazy Loading in Constructor
body: <p>I ran into a strange problem today - I was writing a unit test around a SIM object. It asserts that when the SIM object is updated and the PIN attempts remaining has changed, it will call a particular method. The test looks like this:</p>
  
  <pre><code>[Test]
  public void TestUpdateSimInfoWithPinAttemptsChangedCallsOnPinAttemptsRemaining()
  {
      var info = new SimPinInfo {PinAttemptsRemaining = 10};
  
      var sim = new Mock&lt;Sim&gt;(info);
  
      info.PinAttemptsRemaining = 2;
  
      sim.Object.UpdateSimInfo(info);
  
      sim.Verify(s =&gt; s.FireOnPinAttemptsRemaining(), Times.Once());
  }
  </code></pre>
  
  <p>So the mock SIM object is created with 10 PIN attempts remaining. The <code>SimPinInfo</code> object then has the <code>PinAttemptsRemaining</code> value reduced to 2 before being passed to the <code>UpdateSimInfo()</code> method.</p>
  
  <p>SIM constructor (trimmed for clarity):</p>
  
  <pre><code>internal Sim(SimPinInfo info) : this()
  {
       _pinAttemptsRemaining = info.PinAttemptsRemaining;
       _pukAttemptsRemaining = info.PukAttemptsRemaining;
       ......
  }
  </code></pre>
  
  <p>And the <code>UpdateSimInfo()</code> method (trimmed):</p>
  
  <pre><code>internal void UpdateSimInfo(SimPinInfo info)
  {
      lock(_locker)
      {
          if (_pinAttemptsRemaining != info.PinAttemptsRemaining)
          {
              Log("PinAttemptsRemaining changed");
              _pinAttemptsRemaining = info.PinAttemptsRemaining;
              FireOnPinAttemptsRemaining();
          }
          .....
      }
  }
  </code></pre>
  
  <p>A pretty simple test - what should happen is the above if statement will be true (pin attempts remaining has changed) so the <code>OnPinAttemptsRemaining</code> event will be fired. However, the test failed (although not all the time - it passed when I slowly stepped through the code!). What was happening was the if statement was false - both <code>_pinAttemptsRemaining</code> and <code>info.PinAttemptsRemaining</code> were 2. It appears that the SIM mock is not actually created when expected - when the <code>info.PinAttemptsRemaining</code> was 10.</p>
  
  <p>To demonstrated this, I added a comment:</p>
  
  <pre><code>var sim = new Mock&lt;Sim&gt;(info);
  info.PinAttemptsRemaining = 2;
  Console.WriteLine("SIM's pin attempts = " + sim.Object.PinAttemptsRemaining);
  </code></pre>
  
  <p>I also put a breakpoint in the SIM object's constructor. The breakpoint was hit when stepping over the <code>Console.WriteLine</code> line, not the <code>new Mock...</code> line. So the object is not being created until needed.</p>
  
  <p>I believe this is called lazy-loading or lazy-evaluation.</p>
  
  <p>There were various workaround to this behaviour - I ended up creating a new <code>SimPinInfo</code> object to pass to <code>UpdateSimInfo()</code>.</p>
  
  <p>Has anyone come across this behaviour before? I couldn't find any references to it.</p>