Controlling Non-Public Methods Behavior Based on the Argument Value

You can control a fake Non public method's behavior based on call arguments by requiring all arguments to match the arguments used in the WhenCalled statement.

Syntax
C#

Isolate.NonPublic.WhenCalled(() => <method>).WithExactArguments(<Args_list>).<behavior>;

VB

Isolate.NonPublic.WhenCalled(Function() <method>).WithExactArguments(<Args_list>).<behavior>

Sample:

The following sample shows how to fake the method's return value as follows:

The return value of MethodReturnInt is set to 10 only when the arguments are "typemock" and 1.

The return value of MethodReturnInt is set to 50 only when the arguments are "unit tests" and 2.

C#

[TestMethod, Isolated]
public void FakeNonPublicMethod_BasedOn_ExactMethodArgs()
{
    // As we dont need to fake the object only its method we can use a live object.
    var liveDependency = new Dependency();
    Isolate.NonPublic.WhenCalled(liveDependency, "MethodReturnInt").WithExactArguments("typemock", 1).WillReturn(10);
    Isolate.NonPublic.WhenCalled(liveDependency, "MethodReturnInt").WithExactArguments("unit tests", 2).WillReturn(50);
    
    var result = new Tested().SimpleCalculation(liveDependency);
	
    Assert.AreEqual(60, result);
}

public class Tested
{
    public int SimpleCalculation(Dependency dependency)
    {
        return dependency.CallPrivate("typemock", 1) + dependency.CallPrivate("unit tests", 2);
    }
}

public class Dependency
{
    private int MethodReturnInt(string p, int p_2)
    {
        throw new NotImplementedException();
    }

    public int CallPrivate(string p, int p_2)
    {
        return MethodReturnInt(p, p_2);
    }
}

VB

<TestMethod(), Isolated()> _
Public Sub FakeNonPublicMethod_BasedOn_ExactMethodArgs()
    
    ' As we dont need to fake the object only its method we can use a live object.
    Dim liveDependency = New Dependency()
    Isolate.NonPublic.WhenCalled(liveDependency, "MethodReturnInt").WithExactArguments("typemock", 1).WillReturn(10)
    Isolate.NonPublic.WhenCalled(liveDependency, "MethodReturnInt").WithExactArguments("unit tests", 2).WillReturn(50)

    Dim result = New Tested().SimpleCalculation(liveDependency)
   
   Assert.AreEqual(60, result)
End Sub

Public Class Tested
    Public Function SimpleCalculation(dependency As Dependency) As Integer
        Return dependency.CallPrivate("typemock", 1) + dependency.CallPrivate("unit tests", 2)
    End Function
End Class

Public Class Dependency
    Private Function MethodReturnInt(p As String, p_2 As Integer) As Integer
        Throw New NotImplementedException()
    End Function

    Public Function CallPrivate(p As String, p_2 As Integer) As Integer
        Return MethodReturnInt(p, p_2)
    End Function
End Class