Rhino Mocks - Conditional Expectations

time to read 3 min | 471 words

Here is an interesting usage of Rhino Mocks:

mockAuthenticationSvc = Mocks.CreateMock<IAuthenticationService>();

Expect.Call(mockAuthenticationSvc.IsValidLogin("test", "test"))

    .Return(true).Repeat.Any();

Expect.Call(mockAuthenticationSvc.IsValidLogin(null, null))

    .Constraints(!Is.Equal("test"), !Is.Equal("test"))

    .Return(false).Repeat.Any();

Mocks.ReplayAll();

What does this do?

It set the mock authentication service to accept any test/test name & password pair as valid, and any non test/test as invalid. Now I can start doing asserts based on this information.

For bonus points, what happens if I pass test/foo ?