Today it is a bit cumbersome to check mocked method parameters.
One possibility is the following where the expected value is passed to the delegate via Check.CustomCheck:
public static bool CheckParameter(ParameterCheckerEventArgs data)
{
return data.ArgumentValue == data.ExpectedValue;
}
[Test]
public void CheckParametersWithCustomCheck()
{
MockManager.Init();
Mock mock = MockManager.Mock(typeof(TestedClass));
TestedClass t = new TestedClass();
// setup expectation, test first argument by calling our CheckParameter
// with ExpectedValue set
string expectedParameter = "String";
mock.ExpectCall("SomeMethod").Args(
Check.CustomChecker(
new ParameterCheckerEx(CheckParameter), expectedParameter));
t.SomeMethod("String");
MockManager.Verify();
}
Another possibility is by using anonymous methods like this:
[Test]
public void CheckParametersWithAnonymousMethod()
{
MockManager.Init();
Mock mock = MockManager.Mock(typeof(TestedClass));
TestedClass t = new TestedClass();
// setup expectation, test first argument by calling our CheckParameter
// with ExpectedValue set
string expectedParameter = "String";
mock.ExpectCall("SomeMethod").Args(
new ParameterCheckerEx(delegate(ParameterCheckerEventArgs args)
{
if ((string)args.ArgumentValue == expectedParameter)
return true;
else
return false;
}));
t.SomeMethod("String");
MockManager.Verify();
}
I still think it could be simpler and more intuitive. The developer always have to instantiate the ParameterCheckerEx and possibly the wrap it in a Check.ParameterChecker.
Why not overload the void IParameters.Args(params object[] args) method, so it accepts a delegate?
public void Args(params object[] args) { }
public void Args(ParameterCheckerEx args) { }
public void Args(ParameterCheckerEx args, object expectedValue) { }
So it can be used like this:
[Test]
public void CheckParametersArgsOverloading1()
{
MockManager.Init();
Mock mock = MockManager.Mock(typeof(TestedClass));
TestedClass t = new TestedClass();
// setup expectation, test first argument by calling our CheckParameter
// with ExpectedValue set
string expectedParameter = "String";
mock.ExpectCall("SomeMethod").Args(
delegate(ParameterCheckerEventArgs args)
{
if ((string)args.ArgumentValue == expectedParameter)
return true;
else
return false;
});
t.SomeMethod("String");
MockManager.Verify();
}
[Test]
public void CheckParametersArgsOverloading2()
{
MockManager.Init();
Mock mock = MockManager.Mock(typeof(TestedClass));
TestedClass t = new TestedClass();
// setup expectation, test first argument by calling our CheckParameter
// with ExpectedValue set
string expectedParameter = "String";
mock.ExpectCall("SomeMethod").Args(
delegate(ParameterCheckerEventArgs args)
{
if (args.ArgumentValue == args.ExpectedValue)
return true;
else
return false;
}, expectedParameter);
t.SomeMethod("String");
MockManager.Verify();
}
It hides the complexity and delivers a much clearer interface for parameter checking.