You may not want to expose all private methods to your unit tests, as this can be confusing, so just add a private method for your singleton classes.
public class MySingleton { private void ClearForUnitTest() { Console.WriteLine("Cleared."); } }
Create an extension to be used in your unit tests.
public static class PrivateExtensions { public static void ClearForUnitTest<T>(this T instance) { var method = typeof(T).GetMethod("ClearForUnitTest", BindingFlags.NonPublic | BindingFlags.Instance); method.Invoke(instance, null); } }
Use as if it will be publicly available
private static void Main(string[] args) { var ms = new MySingleton(); ms.ClearForUnitTest(); }
profit
source share