# Question about testing self.inherited

**URL:** <https://www.ruby-forum.com/t/question-about-testing-self-inherited/189975>\
**Category:** Ruby\
**Created:** [June 10, 2010, 6:30pm UTC](https://www.ruby-forum.com/t/question-about-testing-self-inherited/189975 "2010-06-10T18:30:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JesSSSSs\_Gabriel\_y\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4da419/32.png) [@JesSSSSs\_Gabriel\_y\_S](https://www.ruby-forum.com/u/JesSSSSs_Gabriel_y_S)\
**Post date:** [June 10, 2010, 6:30pm UTC](https://www.ruby-forum.com/t/question-about-testing-self-inherited/189975/1 "2010-06-10T18:30:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JesSSSSs\_Gabriel\_y\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4da419/32.png) [@JesSSSSs\_Gabriel\_y\_S](https://www.ruby-forum.com/u/JesSSSSs_Gabriel_y_S)\
**Post date:** [June 10, 2010, 6:40pm UTC](https://www.ruby-forum.com/t/question-about-testing-self-inherited/189975/2 "2010-06-10T18:40:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JesSSSSs\_Gabriel\_y\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4da419/32.png) [@JesSSSSs\_Gabriel\_y\_S](https://www.ruby-forum.com/u/JesSSSSs_Gabriel_y_S)\
**Post date:** [June 10, 2010, 7:17pm UTC](https://www.ruby-forum.com/t/question-about-testing-self-inherited/189975/3 "2010-06-10T19:17:40Z")

</div>

On Thu, Jun 10, 2010 at 6:40 PM, Rein H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 🙂

Jesus.

---

<div class="post-metadata">

**Author:** ![JesSSSSs\_Gabriel\_y\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4da419/32.png) [@JesSSSSs\_Gabriel\_y\_S](https://www.ruby-forum.com/u/JesSSSSs_Gabriel_y_S)\
**Post date:** [June 10, 2010, 7:18pm UTC](https://www.ruby-forum.com/t/question-about-testing-self-inherited/189975/4 "2010-06-10T19:18:15Z")

</div>

On Thu, Jun 10, 2010 at 6:40 PM, Rein H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 🙂

Jesus.
