Skip to main content

Getting Started with FakeItEasy


I've been writing unit tests for a long time now and let me tell you - writing good unit tests are really hard, especially when the system under test (SUT) has several dependencies that have some complex logic in them. 

Some characteristics of good unit tests are:
1. Atomicity - It has to test one thing.
2. Deterministic - It should either Pass/Fail
3. Repeatable - Should yield the same result no matter how many times it runs.
4. Order Independent - Must be able to run in an isolated fashion
5. Fast - Run within milliseconds (in most cases)
6. Easy to Setup

In order to meet these criteria, it's important to address the issues with dependencies. This is where test doubles or mocking frameworks come into the picture. 

What is FakeItEasy?
A .Net dynamic fake framework for creating all types of fake objects, mocks, stubs etc.
Mock objects allow you to mimic the behavior of classes and interfaces, letting the code in the test interact with them as if they were real. They can help you to create fake objects, set fake objects behavior, and verify if the method was called or not.

FakeItEasy is one such framework - other similar frameworks include Rhino.Mock or Moq. One key advantage of FakeItEasy is its easier semantics. There is very little syntax difference in creating Fakes, Mocks, or Stubs.

FakeItEasy is compatible with both C# and Vb.NET. In order to get started - we just have to add the FakeItEasy NuGet Package to our unit test project. This can either be done by right-clicking on the project and clicking "Manage NuGet Packages..." (in visual studio) or by running the below command using the package manager console window.

Install-Package FakeItEasy

Most Units tests (if not all) are written in the AAA format, which stands for Arrange, Act, and Asserts. FakeItEasy can help us in the arrange phase - where fake objects are passed to the SUT. Its built-in extension methods can also simplify code in the assertion stage. 

Let's look at some for some of the features and advantages of using the FakeItEasy Framework

1. Create and pass fake objects
Creating Fake objects can be easily done using Fake<MyObjectType> statement. The created object is then used as a return value from the dependent function. Values passed into methods can be faked using the .ignore (_) syntax

2. Verify method execution and return value
For the assert stage, there are several extension methods in FakeItEasy that will allow us to check if a method is called, how many times its called, and the value that is returned.

3. Verify SUT exception handling
Exceptions occur during the lifetime of an application, therefore it is important to simulate these errors and test the SUT's exception handling mechanism. FakeItEasy simplifies the testing process with the Throws() or ThrowsAsync() functions.

4. Verify Properties
Properties getters and setters can be verified similar to method calls as well.

As you can see FakeItEasy helps to simplify our unit tests and makes it easier to understand because of its easier semantics and fluent syntax.