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
- Set everything in the Mock Engine
- Test your stubbed expectation
bool testValue =
Check to see if your expectations were met
There you go, you now have a stub with expectations set.
*** Note *** Please pay attention to the fact that if you
your expectation will NOT work correctly.
Till next time,
07-25-2007 7:29 AM