Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shim a void method

I have the following (legacy, inherited from predecessors) VB.Net (framework 2.0) Classes and Methods which creates a System Event Log on Exception:

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

I am trying to test this in a C# (framework 4.5) Unit Test project, and part of the logic requires that aValue be two numbers separated by a comma - thereby throwing an exception and event log if not.

I have set up the following Unit Test to ensure that the exception is correctly thrown:

[TestClass]
public class MyClassUnitTest
{
    private readonly StubMyClass myClass = new StubMyClass();

    [TestMethod]
    [ExpectedException(typeof(InvalidCastException))]
    public void TestMyMethodInvalidMissingNumber()
    {
        this.myClass.MyMethod("0,");
    }
}

This all works nicely and the test passes, as the logic in MyMethod causes the expected exception. However, I don't want the Event Log to be written. So, how do I Shim the MyUtitlityClass to intercept the call to LogError and do nothing instead?

All the Fakes examples I have seen are for methods which return a fake value (and are all written in C#!) When I try and implement such a pattern, it gives an error that LogError does not have a getter.

like image 851
Shevek Avatar asked Jul 24 '13 14:07

Shevek


1 Answers

I think you're looking for something along the lines of:

Using context = ShimsContext.Create()
    ShimMyUtilityClass.LogStringStringException =
        Sub(aClass As String, aMethod As String, anException As Exception)
            // YOUR CODE HERE
        End Sub
End Using

Or, in C#:

using (context = ShimsContext.Create()) {
    ShimMyUtilityClass.LogStringStringException =
        (string aClass, string aMethod, Exception anException) => { 
            // YOUR CODE HERE
        };
}
like image 91
jessehouwing Avatar answered Nov 11 '22 07:11

jessehouwing