Question about testing self.inherited

  • Thread starter Jesús Gabriel y Galán
  • Start date
J

Jesús Gabriel y Galán

Hi,

I have a class that keeps track of its subclasses using the self.inherited hook:

module GameFramework
class Game
attr_accessor :event_handler
@available_games = []

class << self
attr_reader :available_games
end

def self.inherited subclass
@available_games << subclass
end
end
end

I want to test this class, but I have a problem testing both the case
when there are no classes inheriting from Game and the case where
there are. I made this naive approach:

class TestGame < MiniTest::Unit::TestCase
def test_available_games_is_empty
assert_empty GameFramework::Game.available_games
end

def test_available_games_has_one
Class.new(GameFramework::Game)
assert_equal 1, GameFramework::Game.available_games.size
end
end

When I run these tests, the second one fails, probably is running
before the other one, defining the class, I guess. Is there a good way
to test these cases that I'm not seeing? Is the above strategy a good
one to keep track of existing subclasses of the Game class? I am using
the available games to create a menu for the user to choose which game
to play.

Thanks,

Jesus.
 
J

Jesús Gabriel y Galán

On 2010-06-10 09:30:24 -0700, Jes=FAs Gabriel y Gal=E1n said


Write a method to clear your available_games array for testing and use it= in
the setup.

Doh, that's pretty straightforward !!!!
Thanks :)

Jesus.
 
J

Jesús Gabriel y Galán

On 2010-06-10 09:30:24 -0700, Jes=FAs Gabriel y Gal=E1n said


Write a method to clear your available_games array for testing and use it= in
the setup.

Doh, that's pretty straightforward !!!!
Thanks :)

Jesus.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top