Quick tip – How to fake a singleton

The Singleton Pattern and How to Fake It

The singleton pattern is well known and used in many software projects. Faking Singleton can be done by replacing the singleton class on creation or by faking the property (or method) that returns the singleton.

image1. Fake the next instance of the class – by using Swap.NextInstance to replace the next object instantiation with our fake class:

 

 

2. Fake the Current/Instance property – If the singleton object created before the test starts we can fake the property/method that returns the singleton instead:

 

It’s that simple…