Using Custom Checks

Brad has just blogged that “Mocking Still Not Quite There For Me“. I looked at his post and thought that this can serve as a good example for using a Custom Checker so here goes:

(Ill post only the test code itself the full working example can be downloaded here)

[TestClass]
public class CustomChecker
{
// Just an empty method
public void VoidEmptyMethod()
{
}

// Our custom Checker
public bool Check(ParameterCheckerEventArgs args)
{
MethodInfo actual = (args.ArgumentValue as MethodInfoWrapper).Method;
MethodInfo expected = GetType().GetMethod("VoidEmptyMethod");
return actual == expected;
}

[TestMethod,VerifyMocks]
public void CustomCheckerTest()
{
//Create a stub of ITestClassCommand (stubClassCmd)
ITestClassCommand stubClassCmd =
RecorderManager.CreateMockedObject<ITestClassCommand>();
//Create a stub of ITestCommand (stubTestCmd)
ITestCommand stubTestCmd = RecorderManager.CreateMockedObject<ITestCommand>();
//Get the MethodInfo for an empty, void-returning method (methInfo)
List<ITestCommand> fakeList = new List<ITestCommand>();
fakeList.Add(stubTestCmd);
using (RecordExpectations rec = new RecordExpectations())
{
//- In response to ShouldCreateInstance, return true
bool dummy = stubTestCmd.ShouldCreateInstance;
rec.Return(true);
//- In response to EnumerateTestCommands, return StubTestCmd
IEnumerable<ITestCommand> dummy2 = stubClassCmd.EnumerateTestCommands(null);
rec.Return(fakeList).CheckArguments(new ParameterCheckerEx(Check));
}
//Act: Call TestCommandFactory.Make, passing stubClassCmd and methInfo
TestCommandFactory.Make(stubClassCmd, GetType().GetMethod("VoidEmptyMethod"));
//Assert:Ensure that the IMethodInfo passed
//into stubClassCmd.EnumerateTestMethod wrapped methInfo
// - this is done by the ParameterCheckerEx


//Ensure that we returned a wrapped ITestCommand
//(test all the levels of wrapping described above)
// - here can other assertion follow
}
}

And the point is the usage of “ParameterCheckerEx”, Using this mechanism one can extend the argument checking mechanism to do almost anything. in our case we checked that the argument passed during the run is the wrapped VoidEmptyMethod MethodInfo.

Last tip. Inside the Check method one can access the “args.ExpectedValue” property that will store the expected value passed during the recording of the call. In our case the null passed here:

IEnumerable<ITestCommand> dummy2 = stubClassCmd.EnumerateTestCommands(null);

That said I do agree that there are cases in which Manual Mocks are more appropriate. In my experience this is usually true to a more simpler kind of scenarios, in which the overhead of using a framework is higher then just writing down the manual mocks. However, time and time again i have seen how quickly those Manual Mocks become a small mini framework, which in my opinion will always be the lesser solution to a full grown mature framework.

TOP