Home > Test Driven Development > Who left the PartialMock out in the sun? It stinks!

Who left the PartialMock out in the sun? It stinks!

Jimmy Bosse Partial Mocks

May 14th 2009 | Jimmy Bosse

Who left the PartialMock out in the sun? It stinks!

I recently paired with a developer to add functionality to an older piece of code. My pair typed in a line of code that almost gave me a heart attack:

ObjectUnderTest myObjectUnderTest =

mocks.PartialMock<ObjectUnderTest>(int someVariable);

I have an aversion to PartialMock like a vampire does to light. Its use is one of the putrid code smells I come across on a regular basis. But before I start a flame war let me preface my post by stating that there are many valid uses of PartialMock. It is very helpful when bringing a large legacy code base into a TDD environment, or when testing inherited objects. But most of the time, it’s more like a big block of gouda baking in a sunny kitchen window for a month or two.
Partial mocks are a sign that you might need to inject a dependency.

Imagine we are testing a Person class:

IPerson person = new Person();
Assert.AreEqual(true, person.IsAlive());

One of my qualifications for being alive is that my person must have a pulse, and luckily there is a HasPulse property on my person, so one might be inclined to:

Person person = mocks.PartialMock<Person>();
Expect.Call(person.HasPulse).Return(true);
mocks.ReplayAll();
bool isAlive = person.IsAlive();
mocks.VerifyAll();
Assert.AreEqual(true, isAlive);

Ugh.

Needing to fake out one part of my class to test another part is a sure sign that my class is doing too much. What my person needs is a heart. If I inject my person with a heart, my person can simply see if the heart is beating instead of asking itself if it has a pulse.

IHeart mockHeart = mocks.DynamicMock();
SetupResult.For(mockHeart.IsBeating).Return(true);
mocks.ReplayAll();
IPerson person = new Person(mockHeart);
bool isAlive = person.IsAlive();
mocks.VerifyAll();
Assert.AreEqual(true, isAlive);

Now when my test passes my person will be properly dependent on a heart. How the heart functions is separate from how my person relies on the heart.

public class Person : IPerson
{
    private readonly IHeart _heart;
    public Person(IHeart heart)
    {
        _heart = heart;
    }
    public bool IsAlive()
    {
        return _heart.IsBeating();
    }
}

Now imagine that my person needed more than a beating heart to survive, like lungs, liver, stomach, etc. If I had put all the code for each of these organs within my person, the class would get extremely complicated. Now I can make each organ function individually, and simply make my person dependent on his organs. So while it may seem like overkill now, with just a beating heart, by avoiding the partial mock in the beginning there is precedence for the next pair who works in this class to inject their organ when the client decides that the person needs a pancreas.

  1. No comments yet.
  1. No trackbacks yet.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: