Jan 20, 2009

Strict Mork vs Non-strict mork

A strict Mock Object will only allow calling methods on it that were explicitly set via Expectations. Any call that is made which either differs by the parameter values defined, or by the method name, will usually be handled by throwing an exception while the method is being called. That means the test fails on the first unexpected method call to a strict mock object.

A Non Strict Mock will allow any call to be made to it even if it was not expected. As long as the call does not require a return value, it will, “make happy noises” and only do what’s necessary for everything in the test to work out. If a method which needs to return a value is called and we did not setup a return value as part of setting up that mock object, a RhinoMocks stub object can return the default value for that method’s return type (0 or null usually). Other frameworks may take different approaches and may throw an exception when the method is not configured to return anything.

A non strict Mock can only fail a test if an expected method was not called. You have to call the VerifyAll() method to find out if such a call is missing from the interaction or the test will pass. You can create Non Strict Mocks using RhinoMocks by calling repository.DynamicMock() instead of calling repository.CreateMock().