I'm a beginner both with Rhino mocks and mocking in general. My unit under test does numerical computation and its results are inexact. Therefore my expectations have to be of the form 'parameter is within a range'. In trying to achieve this, I've discovered some issues with Rhino that I don't know how to resolve.
Here's a simple test, which passes.
[Test]
public void Simple()
{
// Arrange
ICall call = MockRepository.GenerateStrictMock<ICall>();
for (int x = 0; x < 1000; x += 100)
{
call.Expect(c => c.Call(x));
}
// Act
for (int x = 900; x >= 0; x -= 100)
{
call.Call(x);
}
// Assert
call.VerifyAllExpectations();
}
If I change the expectation to use the Arg approach, then this also passes:
call.Expect(c => c.Call(Arg<int>.Is.Equal(x)));
If I use the Matches approach to define a custom constraint, however, the test fails:
call.Expect(c => c.Call(Arg<int>.Matches(p => p == x)));
AnalysisUnitTests.TrackFinderTests.Arg_Matches_x:
Rhino.Mocks.Exceptions.ExpectationViolationException : ICall.Call(900); Expected #0, Actual #1.
ICall.Call(p => (p = value(AnalysisUnitTests.TrackFinderTests+<>c__DisplayClass24).x)); Expected #1, Actual #0.
I presume that this is because the predicate (including x) is evaluated as the matching takes place - in which case x is either undefined or fixed in all instances of the predicate.
The reason I was trying to use .Matches is of course that I wanted to write something like
.Arg<int>.Matches(p => p > x - tolerance && p < x + tolerance)
Is there another way to achieve this? Clearly I can do part of the range checking:
.Arg<int>.Is.GreaterThan(x - tolerance)
So is there a way to 'chain' the constraints? Something like
.Arg<int>.Is.GreaterThan(x - tolerance).AndIs.LessThan(x + tolerance)
Any help gratefully received!