When using a mocking tool like RhinoMocks, one of the
features that it can provide is the ability to A) create a stub of an object
and B) set expectations on Method/Property calls that are part of that
method. Doing this is pretty easy, but
like all things there are some ‘gotcha’s’.
Below is a step by step on how to set expectations on a stub
- Create your mock Repository
MockRepository mocker = new MockRepository();
- Create your stub
ISomeInterface provider = mocker.Stub< ISomeInterface >();
Note that I am using the mocker instance NOT MockRepository.GenerateStub. This is because if you use the static method
on the MockRepository object, you WILL NOT
have your expectations set in the runtime engine correctly.
- Set your expectations
Expect.Call(provider.SomeMethod(string.Empty, string.Empty)).IgnoreArguments().Return(false);
- Set everything in the Mock Engine
mocker.ReplayAll();
- Test your stubbed expectation
bool testValue =
provider.SomeMethod();
- Check to see if your expectations were met
mocker.VerifyAll();
There you go, you now have a stub with expectations set.
*** Note *** Please pay attention to the fact that if you
use MockRepository.GenerateStub
your expectation will NOT work correctly.
Till next time,
Posted
07-25-2007 7:29 AM
by
Derik Whittaker