Controlling Method Behavior Based on the Argument Value Controlling Method Behavior Based on the Argument Value Controlling Method Behavior Based on the Argument Value Controlling Method Behavior Based on the Argument Value Controlling Method Behavior Based on the Argument Value Controlling Method Behavior Based on the Argument Value Controlling Method Behavior Based on the Argument Value Controlling Method Behavior Based on the Argument Value Controlling Method Behavior Based on the Argument Value Controlling Method Behavior Based on the Argument Value Controlling Method Behavior Based on the Argument Value Controlling Method Behavior Based on the Argument Value Controlling Method Behavior Based on the Argument Value Controlling Method Behavior Based on the Argument Value Controlling Method Behavior Based on the Argument Value Controlling Method Behavior Based on the Argument Value Controlling Method Behavior Based on the Argument Value Controlling Method Behavior Based on the Argument Value Controlling Method Behavior Based on the Argument Value Controlling Method Behavior Based on the Argument Value Controlling Method Behavior Based on the Argument Value Controlling Method Behavior Based on the Argument Value

Controlling Method Behavior Based on the Argument Value

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

Syntax
C#

WhenCalled(() => <method>).WithExactArguments().<behavior>;

VB

WhenCalled(Function() <method>).WithExactArguments().<behavior>

Samples
Sample 1: Faking Method’s Return Value

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 FakeReturnValue_BasedOn_ExactMethodArgs()
{
  var fake = Isolate.Fake.Instance<Dependency>();
  Isolate.WhenCalled(() => fake.MethodReturnInt("typemock",1)).WithExactArguments().WillReturn(10);
  Isolate.WhenCalled(() => fake.MethodReturnInt("unit tests", 2)).WithExactArguments().WillReturn(50);

  var result = new ClassUnderTest().SimpleCalculation(fake);
  Assert.AreEqual(60,result);
}
 
public int SimpleCalculation(Dependency dependency)
{
  return dependancy.MethodReturnInt("typemock", 1) + dependancy.MethodReturnInt("unit tests", 2);
}

VB

<TestMethod(), Isolated()> _
Public Sub FakeReturnValue_BasedOn_ExactMethodArgs()
    Dim fake = Isolate.Fake.Instance(Of Dependency)()

    Isolate.WhenCalled(Function() fake.MethodReturnInt("typemock", 1)).WithExactArguments().WillReturn(10)
    Isolate.WhenCalled(Function() fake.MethodReturnInt("unit tests", 2)).WithExactArguments().WillReturn(50)

    Dim result = New ClassUnderTest().SimpleCalculation(fake)
    Assert.AreEqual(60, result)
End Sub

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

Sample 2: Faking a Void Method’s Return Value

The following sample shows how to fake the return value for void methods.

This is the default behavior for indexers, that is each index will return a different value.

C#

[TestMethod, Isolated]
public void FakeVoidMethod_BasedOn_ExactMethodArgs()
{
  var fake = Isolate.Fake.Instance<Dependency>(Members.CallOriginal);
  Isolate.WhenCalled(() => fake.VoidMethod(4)).WithExactArguments().IgnoreCall();
 
  new ClassUnderTest().CallVoid(fake,4);
  // Assert.NoExceptionWasThrown();
}

public void VoidMethod(int n)
{
  throw new NotImplementedException();
}

VB

<TestMethod(), Isolated()> _
Public Sub FakeVoidMethod_BasedOn_ExactMethodArgs()
    Dim fake = Isolate.Fake.Instance(Of Dependency)(Members.CallOriginal)

    Isolate.WhenCalled(Sub() fake.VoidMethod(4)).WithExactArguments().IgnoreCall()

    Dim underTest = New ClassUnderTest()
    underTest.CallVoid(fake, 4)
    ' Assert.NoExceptionWasThrown();
End Sub

Public Sub VoidMethod(n As Integer)
  Throw New NotImplementedException()
End Sub