Over a million developers have joined DZone.

Test Singletons

DZone's Guide to

Test Singletons

Free Resource
Method 1: Two classes. The first implements the functionality. The second gets the protection, i.e. it is the Singleton.

require 'singleton'
class WorkingFoo
  # all the functionality

class SingleFoo < WorkingFoo
  implements Singleton

In production use the second class, SigleFoo. For testing use the first class, WorkingFoo.

Literature: Russ Olso, Design Paterns in Ruby, 2007

Method 2: 
If you don't own the code or if you fear missuse of the baseclass you can use metaprogramming. 
Ian White poposes a #reset_instance method

require 'singleton'
class <
More Info: http://blog.ardes.com/2006/12/11/testing-singletons-with-ruby 


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}