Controlling Method Behavior when Arguments Satisfy Custom Rules

AndArgumentsMatch accepts a predicate in which you call a custom logic to specify whether the method will be faked. To use AndArgumentsMatch, define the types that you want to check as your lambda parameters and pass these as arguments to the fake method.

Syntax
C#

Isolate.WhenCalled((<type arg1>, <type arg2>) => fake.<method> (<arg1>, <arg2>))
  .AndArgumentsMatch((<arg1>, <arg2>) => <check>.<behavior>;

VB

Isolate.WhenCalled(Function(<type arg1>, <type arg2>) fake.<method>(<arg1>, <arg2>))
  .AndArgumentsMatch(Function(<arg1>, <arg2>) <check>.<behavior>

Samples
Sample 1: Using the AndArgumentsMatch Method and Checking All Arguments

The following sample shows how to use AndArgumentsMatch as follows:

An overload of Isolate.WhenCalled() is used to accepts a lambda expression as an argument. The arguments are placeholders for the arguments to be checked.

AndArgumentsMatch uses a delegate to perform the custom checking. The delegate receives the same arguments that Isolate.WhenCalled() receives. Within the custom logic, you can define the behavior that will be used when the actual call to the method is made.

C#

[TestMethod, Isolated]
public void FakeReturnValue_BasedOn_CustomArgumentsChecking()
{
  var fake = Isolate.Fake.Instance<Dependency>();

  Isolate.WhenCalled((string s, int x) => fake.MethodReturnInt(s, x))
    .AndArgumentsMatch((s, x) => s.StartsWith("Gui") && x < 300)
    .WillReturn(1000);

  var result = new ClassUnderTest().CallWithGuitar100(fake);
  // All the arguments match our custom checker - the returned value is faked.
  Assert.AreEqual(1000, result);
}

public int CallWithGuitar100(Dependency dependency)
{
  return dependancy.MethodReturnInt("Guitar", 200);
}

VB

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

    Isolate.WhenCalled(Function(s As String, x As Integer) fake.MethodReturnInt(s, x)).
        AndArgumentsMatch(Function(s, x) s.StartsWith("Gui") AndAlso x < 300).
        WillReturn(1000)

    Dim result = New ClassUnderTest().CallWithGuitar100(fake)
    ' All the arguments match our custom checker - the returned value is faked.
    Assert.AreEqual(1000, result)
End Sub

Sample 2: Using the AndArgumentsMatch Method and Checking Part of Arguments

The following sample shows how to check only some of the method arguments by sending only the arguments to be checked to Isolate.WhenCalled():

Only the second argument of the GetQuota() method (int x) is used in Isolate.WhenCalled().

When the actual method is called, the method is faked based on the value of only the second argument.

C#

[TestMethod, Isolated]
public void FakeReturnValue_BasedOnCustomArgumentsChecking_CheckOneArgument()
{
  var fake = Isolate.Fake.Instance<Dependency>();

  Isolate.WhenCalled((int x) => fake.MethodReturnInt("", x))
    .AndArgumentsMatch( x => x < 300)
    .WillReturn(1000);

  var result = new ClassUnderTest().CallWithGuitar100(fake);
  // All the arguments match our custom checker - the returned value is faked.
  Assert.AreEqual(1000, result);
}

VB

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

    Isolate.WhenCalled(Function(x As Integer) fake.MethodReturnInt("", x)).
	AndArgumentsMatch(Function(x) x < 300).
	WillReturn(1000)

    Dim result = New ClassUnderTest().CallWithGuitar100(fake)
    ' All the arguments match our custom checker - the returned value is faked.
    Assert.AreEqual(1000, result)
End Sub