On object creation and unit tests

There is always a balance between readability and maintainability when writing unit tests.
I hate hearing a fellow developer say – “the task took me X time more because I had to fix 100+ tests because I refactored my production code”. This means I’m always on the look for ways to decrease the tests maintenance overhead.
While adding  (or removing) a parameter to a method can cause the discomfort of fixing a few tests that run that method – doing the same to the constructor is downright painful. The constructor of the class you’re testing is the method that have to run on each test– and if you change it you must edit all of those tests so that they would compile after the change.
Over the years (and projects) I have used several strategies to reduce the pain of change – the following are the ones that worked for me:

Our test subject

I’ve decided to use the old mocking-frameworks-compare project which unfortunately has not been updated for some time now.
The project took several Mocking (read: Isolation) frameworks and compared their API using a simple problem/story.
I have a brain (business logic) that should cause Mouth to Yell if a hot Iron is touched by Hand.
image
Since at the time FakeItEasy was not around to be compared I’ve decided to use to
explain how we can (or rather should) create out test subject.

Option #0 – Do nothing

Been there, done that. From time to time we accept the fact that this is as good as it gets and prepare to pay the cost. I have always preferred readability in my unit tests and I still write tests where I just create the object under test.
Keep it simple means that you get to choose using a simple new to create the object under test.

[TestMethod]
public void TouchHotIron_Yell()
{
var hand = A.Fake();
var mouth = A.Fake();

A.CallTo(() => hand.TouchIron(A.Ignored)).Throws();

var brain = new Brain(hand, mouth);
brain.TouchIron(new Iron { IsHot = true });

A.CallTo(() => mouth.Yell()).MustHaveHappened();
}

Although simple this is not a robust solution, the object creation is tightly coupled to the test which could cause some discomfort – what happens if we add another parameter to the class constructor – sounds innocent enough, until you find yourself fixing 200 tests that happen to instantiate that object. This makes developer shy from adding parameters to constructors (seen that) or create an overload for each new parameter added just to make sure that the test code continue to compile – and then it’s my job to search the code for those useless constructors and delete them one by one.

Option #1 – Use setup method + fields

Each day a software developer who writes unit tests looks at it tests and thinks to himself – I can reduce the duplication by using the Test Setup method (in MSTest TestInitialize) and so that developer happily copies all of the initialization code to a single setup method where he can only change one method when the need arises.

[TestInitialize]
public void CreateObject()
{
_hand = A.Fake();
_mouth = A.Fake();

_brain = new Brain(_hand, _mouth);
}

[TestMethod]
public void TouchHotIron_Yell()
{
A.CallTo(() => _hand.TouchIron(A.Ignored)).Throws();

_brain.TouchIron(new Iron { IsHot = true });

A.CallTo(() => _mouth.Yell()).MustHaveHappened();
}

I have written about why I don’t like about using Setup methods namely they split the test logic and could can become bloated as your write more tests and your class required different initialization for each test.
If you use SetUp/TestInitialize to split the creation and testing of your objects make sure that everyone understand that this method should only be used for that purpose alone. I saw how these methods can become complex and difficult as more and more logic moves there until they are impossible to understand – you’ve been warned!

Option #2 – Factory methods

This used to be my favorite method since I still have all of test inside a single method. Instead of moving the plumbing of the initialization into the SetUp method – why not move it to a method and call it from each test that requires it.

[TestMethod]
public void TouchHotIron_Yell()
{
var hand = A.Fake();
var mouth = A.Fake();

A.CallTo(() => hand.TouchIron(A.Ignored)).Throws();

var brain = CreateBrain(hand, mouth);
brain.TouchIron(new Iron { IsHot = true });

A.CallTo(() => mouth.Yell()).MustHaveHappened();
}

private Brain CreateBrain(IHand hand, IMouth mouth)
{
return new Brain(hand, mouth);
}

This way when I read the test I can see everything that the test does and if I care about how the object was initialized I can dive right in.
Please make sure that you don’t do too much inside this factory method – same rules apply as in production code (Single Responsibility being one of them).
I’ve been part of one project where we’ve created a class to initialize and configure most of our business objects and it helped us make sure that we create them according to the requirements.

Option #3 – Auto-Faking container

I’ve started using Auto-Mocking containers after Typemock has added this functionality to Isolator via Isolate.Fake.Dependencies call.
In order to really decouple the creation of the test subject we need to bring in the big guns.
This is a solution similar to the way that object creation is decoupled from usage inside production code – namely Inversion of Control (IoC) container. Using an Auto-Mocking Container we can delegate the object creation logic to a specified class that is responsible to provide all of the constructor’s parameters and pass in Fake objects whenever such an argument was not specified – it’s that simple.
There’s a great blog post about it by Mark Seemann on the subject – go read it now!
In fact the container I’m using with FakeItEasy was written using Mark’s AutoFixture project.
And so by using Auto-Mocking we can make our test look like this:

private IFixture _container;

[TestInitialize]
public void InitializeContainer()
{
_container = new Fixture()
.Customize(new AutoFakeItEasyCustomization());
}

[TestMethod]
public void TouchHotIron_Yell1()
{
var hand = _container.Freeze<Fake>().FakedObject;
var mouth = _container.Freeze<Fake>().FakedObject;

A.CallTo(() => hand.TouchIron(A.Ignored)).Throws();

var brain = _container.Create();
brain.TouchIron(new Iron { IsHot = true });

A.CallTo(() => mouth.Yell()).MustHaveHappened();
}

Simple? It is once you get used to it.

  1. Use NuGet to add AutoFixture.AutoFakeItEasy,
  2. Create a container using the magic words: .Customize(new AutoFakeItEasyCustomization()). This would make sure that fake objects would be passed to instances created using AutoFixture.
  3. Use the Freeze command to make sure that the container would return them when creating the object – I only do this inside the test so that I can use different configurations for different tests.
  4. And that’s it

I my object would require other parameters that I don’t care about they would be provided automatically (and recursively) by the Auto-Mocking container.
I really like using Auto-Mocking in my tests but I prefer to pass the parameters I’m using to the Create method and so I wrote a simple extensions methods that I can use to do exactly that:

public static class AutoFixtureExtensions
{
public static T FreezeFake(this IFixture fixture)
{
return fixture.Freeze<Fake>().FakedObject;
}

public static T Create(this IFixture fixture, TArg1 arg1, TArg2 arg2)
{
fixture.Inject(arg1);
fixture.Inject(arg2);

return fixture.Create();
}
}

I also wrote a few more extension methods for different number of arguments as well as FreezeFake for those at the team that find this API more to their liking:

[TestMethod]
public void TouchHotIron_Yell2()
{
var hand = _container.FreezeFake();
var mouth = _container.FreezeFake();

A.CallTo(() => hand.TouchIron(A.Ignored)).Throws();

var brain = _container.Create();
brain.TouchIron(new Iron { IsHot = true });

A.CallTo(() => mouth.Yell()).MustHaveHappened();
}

[TestMethod]
public void TouchHotIron_Yell3()
{
var hand = A.Fake();
var mouth = A.Fake();

A.CallTo(() => hand.TouchIron(A.Ignored)).Throws();

var brain = _container.Create(hand, mouth);
brain.TouchIron(new Iron { IsHot = true });

A.CallTo(() => mouth.Yell()).MustHaveHappened();
}

Conclusion

In order to write robust unit tests you have to make sure that your tests would fail only when  a bug is introduced and you definitely want to avoid compilation errors caused by the tests.
It can be very frustrating to have to fix a truckload of tests due to a trivial change – and there are practices and tools to help us avoid exactly that.
Auto-mocking container is one of these tools and since I’ve started using it my productivity has improved tremendously – so why don’t you give it a try today.

Happy coding…

7 thoughts on “On object creation and unit tests”

  1. Thanks, that's a great post !
    Showing the evolution/maturity of your unit-tests over time confirms something I discovered the hard way : writing tests is not that hard …. writing maintainable unit tests requires experience and lots of trial and error.

    I'm definitely not a fan of option #1 (and option #0 obviously !) as they make the tests less explicit and “self-contained”.

    In my tests I currently use some variation of Option #3/ Factory Methods for the System Under Test, with optional parameters that default to a stubbed implementation, which allows me to pass only the parameters that make sense for my test.

    This usually looks like this (with Moq ) :

    private static Brain CreateBrain(IHand hand = null, IMouth mouth = null)
    {
    hand = hand ?? new Mock(MockBehaviour.Strict).Object;
    mouth = mouth ?? new Mock(MockBehaviour.Strict).Object;

    return new Brain(hand, mouth);
    }

    In addition to centralizing construction and easing maintenance, it allows to customize what the stubs do when you don't really care about them (for instance, most mock framework have method calls returning null, which is not a good default in some situations … Note also the use of MockBehaviour.Strict … I like when the tests clearly goes visibly wrong when I forgot to set up something …. instead of getting a NullReferenceException)…. This then becomes a method like that :

    private static Brain CreateBrain(IHand hand = null, IMouth mouth = null)
    {
    if(hand == null){
    var stubHand = new Mock(MockBehaviour.Strict);
    stubAnd.Setup(/* … … make it return non null values etc etc*/);
    }

    mouth = mouth ?? new Mock(MockBehaviour.Strict).Object;
    return new Brain(hand, mouth);
    }

    While I'm a big fan of AutoFixture and Mocking frameworks for the construction of dependencies and auxiliary objects in my tests, I prefr that the creation of the SUT remains under my control, and I strangely like the fact that you suffer a little each time you add a constructor parameter, reminding you that if your class has more than 3 or 4 dependencies, it may have too much responsibility.

    Anyway the whole test maintainability and readability is a really hard problem, and the examples you provide definitely go in the good direction 🙂

    (I did a presentation some months ago about the pain of maintenance of unit tests , and some solutions …. If you are interested, the slides are here 😉 http://fr.slideshare.net/ThibaudDesodt/unit-tests-maintenance-hell)

  2. Great post, I hope you don't mind If i steal those 2 extension methods. Maybe you can add them to the AutoFixture.AutoFakeItEasy lib.

  3. The default behavior is fine in most cases, but in some I want greater control (actually, Moq also allows to say what are the default values returned by Mock objects), and I'd rather specify it myself explicitly rather than accidentally relying on the default behaviour of the framework.
    It's just a matter of tastes I guess 🙂

  4. You're right – it is a matter of personal taste as well as the way you write your code.
    Having said that I found that 90% of the time returning a fake “automatically” creates more robust tests.

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