title: Shim a void method
body: <p>I have the following (legacy, inherited from predecessors) VB.Net (framework 2.0) Classes and Methods which creates a System Event Log on Exception:</p>
  
  <pre class="lang-vb prettyprint-override"><code>Public Interface IMyClass
  
      Function MyMethod(ByVal aValue As String) As Date
  
  End Interface
  
  Public Class MyClass
  
      Implements IMyClass
  
      Public Function MyMethod(ByVal aValue As String) As Date Implements IMyClass.MyMethod
  
          Try
  
              ' Calculate return date based on aValue
  
          Catch ex As Exception
              Call MyUtilityClass.LogError("MyClass", "MyMethod", ex)
              Throw
          End Try
  
      End Function
  
  End Class
  
  Partial Public NotInheritable Class MyUtilityClass
  
      Public Shared Sub LogError(ByVal className As String,
                                 ByVal methodName As String,
                                 ByVal ex As Exception)
  
          ' Write details to Event Log
      End Sub
  
  End Class
  </code></pre>
  
  <p>I am trying to test this in a C# (framework 4.5) Unit Test project, and part of the logic requires that <code>aValue</code> be two numbers separated by a comma - thereby throwing an exception and event log if not.</p>
  
  <p>I have set up the following Unit Test to ensure that the exception is correctly thrown:</p>
  
  <pre><code>[TestClass]
  public class MyClassUnitTest
  {
      private readonly StubMyClass myClass = new StubMyClass();
  
      [TestMethod]
      [ExpectedException(typeof(InvalidCastException))]
      public void TestMyMethodInvalidMissingNumber()
      {
          this.myClass.MyMethod("0,");
      }
  }
  </code></pre>
  
  <p>This all works nicely and the test passes, as the logic in <code>MyMethod</code> causes the expected exception. However, I don't want the Event Log to be written. So, how do I Shim the <code>MyUtitlityClass</code> to intercept the call to <code>LogError</code> <em>and do nothing instead?</em></p>
  
  <p>All the Fakes examples I have seen are for methods which return a fake value (and are <em>all</em> written in C#!) When I try and implement such a pattern, it gives an error that LogError does not have a getter.</p>