Thursday, February 15, 2007

all about the mocks

i love mocks. i really do. if i could, i would marry mocks, have lots of mock babies, be a grandmock; you get the idea...

the top reasons i love mocks:
1. focus only on what you are testing
2. other classes won't break your tests
3. create new interfaces and use them before they exist
4. use functionality without knowledge
5. i hate math
6. keep the file system, networks and other real resources out of your unit tests

i'll post examples of each of these reasons

a lot of black box testers have valid reasons for disliking mocks, but the benefits surpass any downside i have ever read or encountered. i use rhino mocks, which uses reflection, so some major downsides of mocks, such as difficult refactoring and type casting are completely avoided.

did i mention i love mocks?

thank you rhino mocks!!!

One other must with mocks is dependency injection. In the past I've written my own object management framework to handle injection, but we've been using the castle framework and its totally excellent, i love this too!!! It takes away all the thinking of dependency injection and lets you focus only on your tests and interfaces. An absolute must!!

3 comments:

lukemelia said...

If you married mocks, wouldn't your home life feel a little... fake?

If you had little mock babies, could you simply expect them to talk and know if advance what they would say?

wendy said...

hmm... I'd guess I'd have to make my mock babies dynamic mocks, to let them have some freedom... but you bring up a good point:

_myBaby = _mocks.DynamicMock<IBaby>();
_myBaby.Cry();
LastCall.Repeat.Never();

I'd actually get some sleep! Though, if the baby never cried I wouldn't know if anything was wrong, which would make me a pretty bad mom... hopefully, the my _mockHusband will have been programmed well

lukemelia said...

Assert.AreEqual(WakingState.SleepingLightly, _mockHusband.WakingState);
_mockBaby.Crying += null;
EventRaiser crier = LastCall.IgnoreArguments().GetEventRaiser();
_mockHusband.Sooth(_mockBaby);
_mockBaby.Sleep();
FinalizeMocks();
crier.Raise();
VerifyAll();
Assert.AreEqual(WakingState.StillSleepingSoundly, _mockWendy.WakingState)