Mixing WithExactArguments and Custom Checkers
To mix custom checkers and WithExactArguments, do not define the exact arguments as a lambda parameter and just pass the matching argument. Typemock Isolator checks the arguments as follows:
• If the arguments have a custom checker, the custom checker is used.
• The arguments that don't have a custom checker are matched exactly.
Syntax
C# Isolate.WhenCalled((<type arg1>, <type arg1>) => fake.<method> (<arg1>, <arg2>, <additional_arguments>)) .AndArgumentsMatch((<arg1>, <arg2>) => <check>).<behavior>;
VB
Isolate.WhenCalled(Function(<type arg1>, <type arg1>) fake.<method>(<arg1>, <arg2>, <additional_arguments>))
.AndArgumentsMatch(Function(<arg1>, <arg2>) <check>).<behavior>
Samples
Sample 1: Using Custom Checks
The following sample shows how to use the custom checkers as follows:
• A custom checker is used only on the second argument of the GetQuote() method.
•WithExactArguments is used against the first argument so only a call to GetQuote() with the string "Guitar" is faked.
When you use AndArgumentsMatch on a chain of calls, all the arguments in the custom checker should belong to one method.
C# [TestMethod, Isolated] public void FakeReturnValue_BasedOn_MixedChecker() { var fake = Isolate.Fake.Instance<Dependency>(); Isolate.WhenCalled((int x) => fake.MethodReturnInt("Guitar", x)) .AndArgumentsMatch(x => x < 300).WithExactArguments().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 class ClassUnderTest { public int CallWithGuitar100(Dependency dependency) { return dependency.MethodReturnInt("Guitar", 200); } } public class Dependency { public virtual int MethodReturnInt(string arg1, int arg2) { throw new NotImplementedException(); } }
VB
<TestMethod(), Isolated()>
Public Sub FakeReturnValue_BasedOn_MixedChecker()
Dim fake = Isolate.Fake.Instance(Of Dependency)()
Isolate.WhenCalled(Function(x As Integer) fake.MethodReturnInt("Guitar", x)).
AndArgumentsMatch(Function(x) x < 300).WithExactArguments().
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
Public Class ClassUnderTest
Public Function CallWithGuitar100(dependency As Dependency) As Integer
Return dependency.MethodReturnInt("Guitar", 200)
End Function
End Class
Public Class Dependency
Public Overridable Function MethodReturnInt(arg1 As String, arg2 As Integer) As Integer
Throw New NotImplementedException()
End Function
End Class
Sample 2: Making a Call that Throws TypeMockException
The following sample shows how to make a call that throws an exception. The arguments passed to Isolate.WhenCalled() are spread across two methods. This causes Typemock Isolator to throw the exception.
C# Isolate.WhenCalled((string s, int i) => fake.GetSon(s).DoSomething(i)) .AndArgumentsMatch((s, i) => s != null && i > 0).WillThrow(new Exception("Simulated")));
VB
Isolate.WhenCalled(Function(s As String, i As Integer) fake.GetSon(s).DoSomething(i)).
AndArgumentsMatch(Function(s, i) s! = Nothing AndAlso i > 0).WillThrow(New Exception("Simulated"))