# Where is my TestResult?

**URL:** <https://rubytalk.org/t/where-is-my-testresult/23710>\
**Category:** ruby-talk\
**Created:** [31 December 2005 11:09 UTC](https://rubytalk.org/t/where-is-my-testresult/23710 "2005-12-31T11:09:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cgmjr](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@cgmjr](https://rubytalk.org/u/cgmjr)\
**Post date:** [31 December 2005 11:09 UTC](https://rubytalk.org/t/where-is-my-testresult/23710/1 "2005-12-31T11:09:48Z")

</div>

So I'm teaching myself Ruby. I'm playing with Test::Unit. I've  
progressed to:

class TC\_venueCreate \< Test::Unit::TestCase  
…  
&nbsp;&nbsp;def test\_Nil  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_nil(@v)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue Test::Unit::AssertionFailedError  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Ha ha! It ain't nil!"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;???TestResult.failures.each {|f| puts f.short\_display }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
…  
end

Where is that TestResult?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Pit](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pit](https://rubytalk.org/u/Pit)\
**Post date:** [31 December 2005 18:49 UTC](https://rubytalk.org/t/where-is-my-testresult/23710/2 "2005-12-31T18:49:18Z")

</div>

cgmjr schrieb:

> So I'm teaching myself Ruby.

Welcome!

> I'm playing with Test::Unit. I've progressed to:
> 
> class TC\_venueCreate \< Test::Unit::TestCase  
> ....  
> &nbsp;&nbsp;def test\_Nil  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_nil(@v)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue Test::Unit::AssertionFailedError  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Ha ha! It ain't nil!"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;???TestResult.failures.each {|f| puts f.short\_display }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> ....  
> end
> 
> Where is that TestResult?

Maybe the following code helps a little bit:

&nbsp;&nbsp;&nbsp;require "test/unit"  
&nbsp;&nbsp;&nbsp;require "enumerator"

&nbsp;&nbsp;&nbsp;# the code is in the form of a unit test, just like yours  
&nbsp;&nbsp;&nbsp;class TestResultSearch \< Test::Unit::TestCase

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# returns all known instances of the given class  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def instances\_of klass  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ObjectSpace.enum\_for( :each\_object, klass ).to\_a  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# returns all classes with the name "TestResult"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def testresult\_classes  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instances\_of( Class ).select { |c| c.name =~ /\bTestResult$/ }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# this test succeeds, so there is exactly one class named  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# "TestResult", but see the next test  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def test\_find\_class  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal 1, testresult\_classes.size  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# this test doesn't succeed, so the TestResult class doesn't have a  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# method named "failures". Look at the error message to find the  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# full name of the TestResult class  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def test\_access\_to\_failures\_via\_class  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testresult\_class = testresult\_classes[0]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_respond\_to testresult\_class, :failures  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# returns all instances of the TestResult class  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def testresult\_instances  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instances\_of Test::Unit::TestResult  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# this test succeeds, so there seems to be exactly one instance of  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# the TestResult class  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def test\_find\_instance  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal 1, testresult\_instances.size  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# this test doesn't succeed, so the instances of the TestResult  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# class don't have a method named "failures", either. Look at the  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# error message to find the instance variables of the TestResult  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# instance  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def test\_access\_to\_failures\_via\_instance  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testresult\_instance = testresult\_instances[0]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_respond\_to testresult\_instance, :failures  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# without reading the Test::Unit source code, the only way to get at  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# the current list of failures I found is to look for the one  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# instance of TestResult and to access its "@failures" instance  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# variable  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def test\_access\_to\_failures\_instance\_variable  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testresult\_instance = testresult\_instances[0]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;failures = testresult\_instance.instance\_variable\_get :@failures  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_not\_nil failures  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;end

The question is: what do you want to do with the current list of failures?

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![cgmjr](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@cgmjr](https://rubytalk.org/u/cgmjr)\
**Post date:** [31 December 2005 19:37 UTC](https://rubytalk.org/t/where-is-my-testresult/23710/3 "2005-12-31T19:37:55Z")

</div>

Excellent, Pit! Many new nuggets of Rubyness for me to chew on here,  
many thanks for taking the time on this Holiday to reply.

> The question is: what do you want to do with the current list of failures?

As I mentioned, I'm exploring Test::Unit to better understand what's  
going on under the surface. I don't have a particular need other than  
familarization and dexterity with Ruby.

However, it seems that one must be able to locate the instance of  
Test::Unit::TestResult in order to invoke it's #add\_observer() method,  
ja? Or perhaps there is a more direct way to Observe what is  
happening? Again, I'm just curious at this point, but some context of  
my project might help:

I wish to create a Ticket Box Office for my wife's theater company. It  
is a facinating little problem, involving a neat little Domain Model of  
Venues, Performances, Seats, Reservations, and the like. I'm starting  
my Ruby journey by implementing a core concept, the Venue. It will be  
a simple CRUD use case/user story(ies), but will lead to such fun as  
SeatMaps and whatnot. Once I feel like I have sufficent understanding  
of the Standard API, I'll move on to Rails and let the web fun begin!

Many thanks again for the code example...\*runs of to start chewing\*...  
g.
