Devin Mullins
6/27/2005 9:09:00 PM
Joe,
In general, get Michael Feathers's Working Effectively with Legacy Code. It talks about 1)how to unit test singletons, and 2)why you shouldn't use singletons.
In specific, find a way to instatiate the class! This could be as easy as sticking this in your test code:
class SingletonToBeTested
public :new
end
There's also testing the code that uses a singleton. You can test the side effects, as long as the singleton doesn't touch a database or something. You can also implement a mock. Michael Feathers suggests the following: at a public setInstance() method to your singleton class, and make the constructor protected. In your testing infrastructure, subclass your singleton with a mock class with public constructor, and pass an instance of that subclass to setInstance whenever you need to.
In Ruby, though, it's quite a bit simpler:
class SingletonToBeTested
attr_accessor :instance
end
class MockSingleton < SingletonToBeTested
...
end
Err... was that what you were after?
Devin