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.