Singleton Unit Testing - A Different Approach
Peli has posted a tip concerning singleton objects and how them being singletons interferes with the testing itself. He also added a solution to this problem. Respect to Peli for this very cool post. :)
At first, I failed to understand his method, so I debated him a bit in his comments. Eventually, I understood what he meant - he created an instance manually each time he set-up his tests.
Here's another way of doing this, if you have some instantiation code in your get accessor for Instance: Just nullify the static instance.
Here's some code that shows how:
public class Singleton
private static Singleton m_Instance;
// Do Initialization Magic
public static Singleton Instance
if (m_Instance == null)
m_Instance = new Singleton();
// Do Instance Getting Magic
public class MyTests
public void TearDown()
System.Reflection.FieldInfo fi =
An added bonus for this method is the ability to use the Singleton class as it was intended for it to be used. There might be some code in the get accessor that should run whenever it is accessed, regardless for the value of the private instance.