Over a million developers have joined DZone.

Test Singletons

·
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
end

class SingleFoo < WorkingFoo
  implements Singleton
end

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 

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}