Why you should start using Strict fakes in FakeItEasy library for .Net

When you create a fake object using FakeItEasy, it allows you to configure the behavior of its methods and properties. By default, not configured calls to these members return default values, for example a method call will return null. This behavior can make your unit-tests perform unexpectedly, since you will be testing only with the returned default value. If you want to enforce stricter behavior during testing, then strict fakes come into play.

A strict fake in FakeItEasy ensures that all calls to unconfigured members throw an ExpectationException. In other words, if you defined a strict fake and forget to configure a method call, your test will fail with an exception.

Here is how you create a strict fake:

var foo = A.Fake<IFoo>(x => x.Strict());

How to configure void methods?

Your code in test can contain methods which do not return any value. If you are defining your fakes as Strict you will also have to configure the behavior of these methods. The DoesNothing method of FakeItEasy is available for void methods. It allows you to configure a method to do nothing when called.

For example:

A.CallTo(() => fake.SomeMethodThatShouldDoNothing()).DoesNothing();

Using Strict fakes saved enormous amount of time, since I am now invest less time on debugging my unit-tests. I greatly suggest you to start defining you fakes as Strict.

comments powered by Disqus