Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing with singletons

I have prepared some automatic tests with the Visual Studio Team Edition testing framework. I want one of the tests to connect to the database following the normal way it is done in the program:

string r_providerName = ConfigurationManager.ConnectionStrings["main_db"].ProviderName; 

But I am receiving an exception in this line. I suppose this is happening because the ConfigurationManager is a singleton. How can you work around the singleton problem with unit tests?


Thanks for the replies. All of them have been very instructive.

like image 533
yeyeyerman Avatar asked Jan 18 '10 12:01

yeyeyerman


People also ask

How do you unit test a singleton class?

First of all, extract an interface from the Singleton class and then apply Dependency Injection. We need interface for decoupling purpose and unit testing. Accessing singleton class from another class, the instance would then be passed to any objects that might trying to implement it through the property.

Why are singletons difficult to unit test?

It's very difficult to write unit tests for code that uses singletons because it is generally tightly coupled with the singleton instance, which makes it hard to control the creation of singleton or mock it.

Can we write unit test for singleton class?

Making a class a singleton can make it difficult to test its clients, as it's impossible to substitute a mock implementation for a singleton unless it implements an interface that serves as its type.

How do singleton affect testing?

If your singleton contains mutable state that cannot be reset between tests, then it will affect unit testing as the tests will need to be run in a specific order and the state accounted for. If your singleton contains state that changes of time, it will prevent you reliably running tests in parallel.


1 Answers

Have a look at the Google Testing blog:

  • Using dependency injection to avoid singletons
  • Singletons are Pathological Liars
  • Root Cause of Singletons
  • Where have all the Singletons Gone?
  • Clean Code Talks - Global State and Singletons
  • Dependency Injection.

And also:

  • Once Is Not Enough
  • Performant Singletons

Finally, Misko Hevery wrote a guide on his blog: Writing Testable Code.

like image 57
Gregory Pakosz Avatar answered Sep 19 '22 07:09

Gregory Pakosz