##Introduction## Fake objects allows to unit test components individually by faking the behavior of dependent components. Fake it easy is a framework which allows to create fake objects effectively and the API of the framework is very easy to understand. This post takes a look into some handy techniques to make unit testing easy with fake it easy.
We have a Car and an Engine as follows.
The engine is injected into the car through the constructor. Intention of the unit test is to ensure than when some body calls Car.Start(), Engine.Start() should be called when the engine is not running.
Since we are testing the implementation details of the Car, we don’t need to worry about the Engine here, so we could simple create a fake engine and use it in our test.
##Testing With Fake Engine##
Let’s go through the above code line by line.
- Creating a fake engine by providing the interface to Fake it easy.
- Saying to fake it easy, “If someone invokes IsRunning() return false always.”
- Creating a Car object with the fake engine.
- Starting the Car.
- Asserting to check weather Start() method is invoked in the fake engine.
We have isolated the engine from our test case and tested the logic of our car class successfully.
##Fake It Easy Quick Reference##
Creating a fake object using an interface.
Returning when a method is invoked on the fake object.
Assigning Ref and Out Parameters. The result variable will be assigned with “success”.
Asserting weather a method was invoked.
Asserting weather a method was invoked exactly once.
Asserting weather a method was invoked with particular parameter where the parameter is a complex object.