title: C# Unit Test - To mock, stub or use explicit implementation
body: <p>This has been discussed a number of times before, but the merits in the below examples aren't obvious, so please bear with me.</p>
  
  <p>I'm trying to decide whether to use mock implementations in my unit tests and am undecided given the following two examples, the first using NSubstitute for mocking and the second using a SimpleInjector (Bootstrapper object) resolved implementation.</p>
  
  <p>Essentially both are testing for the same thing, that the Disposed member is set to true when the .Dispose() method is called (see implementation of method at the bottom of this post).</p>
  
  <p>To my eye, the second method makes more sense for regression testing as the mock proxy explicitly sets the Disposed member to be true in the first example, whereas it is set by the actual .Dispose() method in the injected implementation.</p>
  
  <p>Why would you suggest I choose one over the other for verifying that the method behaves as expected?  I.e. that the .Dispose() method is called, and that the Disposed member is set correctly by this method.</p>
  
  <pre><code>    [Test]
      public void Mock_socket_base_dispose_call_is_received()
      {
          var socketBase = Substitute.For&lt;ISocketBase&gt;();
          socketBase.Disposed.Should().BeFalse("this is the default disposed state.");
  
          socketBase.Dispose();
          socketBase.Received(1).Dispose();
  
          socketBase.Disposed.Returns(true);
          socketBase.Disposed.Should().BeTrue("the ISafeDisposable interface requires this.");
      }
  
      [Test]
      public void Socket_base_is_marked_as_disposed()
      {
          var socketBase = Bootstrapper.GetInstance&lt;ISocketBase&gt;();
          socketBase.Disposed.Should().BeFalse("this is the default disposed state.");
          socketBase.Dispose();
          socketBase.Disposed.Should().BeTrue("the ISafeDisposable interface requires this.");
      }
  </code></pre>
  
  <p>For reference the .Dispose() method is simply this:</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
      /// &lt;/summary&gt;
      public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
  
      /// &lt;summary&gt;
      /// Releases unmanaged and - optionally - managed resources.
      /// &lt;/summary&gt;
      /// &lt;param name="disposeAndFinalize"&gt;&lt;c&gt;true&lt;/c&gt; to release both managed and unmanaged resources; &lt;c&gt;false&lt;/c&gt; to release only unmanaged resources.&lt;/param&gt;
      protected void Dispose(bool disposeAndFinalize)
      {
          if (Disposed)
          {
              return;
          }
  
          if (disposeAndFinalize)
          {
              DisposeManagedResources();
          }
  
          DisposeUnmanagedResources();
  
          Disposed = true;
      }
  </code></pre>
  
  <p>Cheers</p>