Mocking a Singleton
Mocking a Singleton
Submitted by Developer Testing on Sun, 30/04/2006 - 22:59.Over on the TDD mailing list there is a thread about mocking a singleton. There are a few good suggestions given but the best answer (imho) is that provided by Michael Hill:
Mocking a singleton is relatively easy.
To begin with, singleton's are all alone in the world; don't be afraid to point this out. Also, most singletons are created, get this, not even in a factory, but merely in a factory method. Finally there is the point that they are little more than dressed-up globals, and IME experience cause more heartache than they're worth. You can point to the many nasty screeds about singletons there are out on the net. All in all, mocking it shouldn't be too hard. A word: if your singleton has unusually large body parts, stoop to insulting that. Sometimes, to be kind, you have to be cruel.
