Minimal Rhino Mocks sample

var myMock = MockRepository.GenerateMock();
Console.WriteLine(myMock.Execute(“Hello”) ?? “”);

MockRepository mocks = new MockRepository();

var myMock2 = mocks.Stub();
using (mocks.Record())
{
SetupResult.For(myMock2.Execute(“Hello”)).Return(“World”);
}
Console.WriteLine(myMock2.Execute(“Hello”) ?? “”);

I still wonder why mocking frameworks are so popular. Handcrafted mocks seem to do the job quite well and are easy to implement as:

Stub – Ignore inputs and return as little as possible (I frequently return 42 as an integer result)
Spy – Record and log callers.
Saboteur – fail in a controlled fashion (i.e. a stream that will fail with an exception upon the fifth byte read).

If your interfaces are too big then you are not following SOLID properly. If changing the interface causes too many problems then you have not isolated the tests from the code enough (factories and helper methods).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s