Question

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.

Was it helpful?

Solution

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
        };
}

OTHER TIPS

First you need to create a fakes assembly of the assembly which contains MyUtilityClass. Then using the shimmed instance of the MyUtility class mock the LogError method. The following may help:

        using (ShimsContext.Create())
        {   
            ShimMyUtilityClass temp = new ShimMyUtilityClass();
            temp.LogStringStringException = ....
            this.myClass.MyMethod("0,");
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top