The EasyMock framework (mocks for Java) has very clever method createNiceMock, it is:
Creates a mock object that implements the given interface, order checking is disabled by default, and the mock object will return 0, null or false for unexpected invocations.
I wonder about some equivalent method in Rhino Mocks framework that can mocks with 0, null or false for unexpected invocations (I am not interesting in order checking but if it will be it will OK too)
Those are characteristics of dynamic mock:
Dynamic Mock - Loose replay semantics. Created by calling DynamicMock()
Loose replay semantics: All method calls during the replay state are accepted. If there is no special handling setup for a given method, a null or zero is returned. All of the expected methods must be called for the object to pass verification.
In earlier versions of you had to explicitly create one:
var mocks = new MockRepository();
var service = mocks.DynamicMock<IService>();
Nowadays, by default mocks are assumed to be dynamic mocks as long as you create them with MockRepository.GenerateMock<T>()
method. Expectations call order is also irrelevant. Assuming we got expectations set up as below:
var dependency = MocksRepository.GenerateMock<IDependency>();
dependency.Expect(d => d.SecondMethod());
dependency.Expect(d => d.FirstMethod());
Tested code like the example below
public void DoWork()
{
this.Dependency.FirstMethod();
this.Dependency.SecondMethod();
this.Dependency.ThirdMethod();
}
won't cause the test to fail. Order doesn't matter, unexpected invocations are ignored and return default values.