From: Gennady Date: 2004-05-08T01:09:53+09:00 Subject: Re: test unit & singleton That's why you want to avoid singletons as much as possible when it comes to unit testing. In cases I do need singletons, I usually end up with a regular class that can be unit tested, and a separate singleton class that simply delegates all calls to my actual class. Or something similar. Gennady. Guillaume Marcais wrote: > How do you get a fresh copy of a singleton between 2 unit test? It > somewhat breaks the principle of a singleton object as I would like to > get a second brand new instance of the object. > > Example (pseudo-code): > > require 'singleton' > > class MyObject > include Singleton > > # ... Bunch of good stuff > end > > if $0 == __FILE__ > require 'test/unit' > > class MyObject_Test > def test_functionality_1 > o = MyObject.instance > # ... Tests on o, which changes the internal state of o ... > end > > def test_functionality_2 > o = MyObject.instance # This instance is polluted > # by previous tests > # ... Tests on o > end > end > end > > How would I get rid of the instance created and polluted in test 1 to > start anew in test 2? Should I implement a 'reset' method to clear all > states in the singleton object? > > Guillaume. > > > >