Case insensitive waits in Rhino Mocks
I am using Rhino Mocks to wait for a call. There is only one parameter, which is a string. But I am not worried about the string case. I want the test to pass even if the case is wrong. So I am doing the following:
//expect log message to be called with a string parameter.
//We want to ignore case when verifying so we use a constraint
//instead of a direct parameter
Expect.Call(delegate { logger.LogMessage(null); })
.Constraints(Is.Matching<string>(x => x.ToLower()=="f2"));
It seems a little too long. Is there a smarter way to do this?
+2
source to share
2 answers
// arrange
var loggerStub = MockRepository.GenerateStub<ILogger>();
// act
loggerStub.LogMessage("f2");
// assert
loggerStub.AssertWasCalled(
x => x.LogMessage(Arg<string>.Matches(
s => string.Equals(s, "f2", StringComparison.OrdinalIgnoreCase)
))
);
If you don't like parameters but only method calls:
loggerStub.AssertWasCalled(
x => x.LogMessage(null),
x => x.IgnoreArguments()
);
+2
source to share
I would use the AAA format that @ Darin suggests (or similar). I think this will be more concise, but I think you still have to use the same basic constraint for case insensitivity. A helper method can make this more readable.
private bool CaseInsensitive( string s, string t )
{
return string.Equals( s, t, StringComparison.OrdinalIgnoreCase );
}
var loggerMock = MockRepository.GenerateMock<Logger>();
loggerMock.Expect( l => l.LogMessage( Arg<string>.Matches( s => CaseInsensitive( s, "f2" ))));
classUnderTest.MethodUnderTest();
loggerMock.VerifyAllExpectations();
+1
source to share