Rhino Mocks - Conditional Expectations
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 ?
Comments
It barfs and throws us a nice exception.
So how does one set it up so it doesnt throw the exception and returns false for test/foo?
Well, you can't do it with constraints. They can verify just one parameter, you cannot verify dependencies between parameters.
The options you have is to provide expectations for all the premutations (4 in this case) or to use a callback, like this:
Expect.Call(authenticationService.IsValid(null, null))
Comment preview