Question about testing self.inherited

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.

On 2010-06-10 09:30:24 -0700, Jesús Gabriel y Galán said

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.

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

On Thu, Jun 10, 2010 at 6:40 PM, Rein H. [email protected] wrote:

Jesus.

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

Doh, that’s pretty straightforward !!!
Thanks :slight_smile:

Jesus.

On Thu, Jun 10, 2010 at 6:40 PM, Rein H. [email protected] wrote:

Jesus.

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

Doh, that’s pretty straightforward !!!
Thanks :slight_smile:

Jesus.