test unit & singleton

  • Thread starter Guillaume Marcais
  • Start date
G

Guillaume Marcais

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.
 
G

Gennady

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.
 
N

Nathaniel Talbott

That's why you want to avoid singletons as much as possible when it
comes to unit testing.

<tongue in="cheek">Perhaps we should rename singleton.rb to
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.

That would be my suggestion as well.


Nathaniel
Terralien, Inc.

<:((><
 
G

Guillaume Marcais

Not really the answer I prefer :(

I'll follow your advice.

Thanks,
Guillaume.
 
K

Kent Dahl

Gennady said:
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.

Suggestion for something similar, w/o the delegation overhead:

Move all functionality of the singleton class into a module. Include
that module in two classes, one which also includes Singleton (for
actual use) and one dummy class for testing. Put the code for the dummy
class in the unit tests themselves, so the application code doesn't
accidentally use it for anything.

require 'singleton'
module RealStuff
# ...
end
class DummyClass
include RealStuff
end
class RealClass
include RealStuff
include Singleton
end
 
G

Guillaume Marcais

Thanks to all of you for good suggestions.

Guillaume.


Le 8 mai 04, à 05:03, Kent Dahl a écrit :
Gennady said:
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.

Suggestion for something similar, w/o the delegation overhead:

Move all functionality of the singleton class into a module. Include
that module in two classes, one which also includes Singleton (for
actual use) and one dummy class for testing. Put the code for the
dummy class in the unit tests themselves, so the application code
doesn't accidentally use it for anything.

require 'singleton'
module RealStuff
# ...
end
class DummyClass
include RealStuff
end
class RealClass
include RealStuff
include Singleton
end

--
(\[ Kent Dahl ]/)_ _~_ _____[ http://www.pvv.org/~kentda/
]_____/~
))\_student_/(( \__d L b__/ Master of Science in Technology )
( \__\_õ|õ_/__/ ) _) Industrial economics and technology management (
\____/_ö_\____/ (____engineering.discipline_=_Computer::Technology___)
 
C

Christoph

Guillaume said:
Thanks to all of you for good suggestions.

Here is another possiblity

---
require 'singleton'

class MySingletonClass
include Singleton
end

# use or test MySingletonClass, in particular
# instantiate it ...

my_inst = MySingletonClass.instance

# make a temporary copy and remove
# the current MySingletonClass constant
# and reassign it the uninstantiated
# temporary copy. The exmaple assumes that
# MySingletonClass is defined in the top scope,
# furthermore there are obvious complications
# if your stuff depends on the inheritance capility
# of the Singleton implementation ..



class Object
tmp = MySingletonClass.clone
remove_const :MySingletonClass
const_set :MySingletonClass, tmp
end


# MySingletonClass is an uninstantiated at
# this point ...

p (my_inst == MySingletonClass.instance) # false
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top