Where is my TestResult?


#1

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

class TC_venueCreate < Test::Unit::TestCase

def test_Nil
begin
assert_nil(@v)
rescue Test::Unit::AssertionFailedError
puts “Ha ha! It ain’t nil!”
???TestResult.failures.each {|f| puts f.short_display }
raise
end
end

end

Where is that TestResult?

Thanks in advance


#2

cgmjr schrieb:

So I’m teaching myself Ruby.

Welcome!

     raise
   end

end

end

Where is that TestResult?

Maybe the following code helps a little bit:

require “test/unit”
require “enumerator”

the code is in the form of a unit test, just like yours

class TestResultSearch < Test::Unit::TestCase

 # returns all known instances of the given class
 def instances_of klass
   ObjectSpace.enum_for( :each_object, klass ).to_a
 end

 # returns all classes with the name "TestResult"
 def testresult_classes
   instances_of( Class ).select { |c| c.name =~ /\bTestResult$/ }
 end

 # this test succeeds, so there is exactly one class named
 # "TestResult", but see the next test
 def test_find_class
   assert_equal 1, testresult_classes.size
 end

 # this test doesn't succeed, so the TestResult class doesn't have a
 # method named "failures". Look at the error message to find the
 # full name of the TestResult class
 def test_access_to_failures_via_class
   testresult_class = testresult_classes[ 0 ]
   assert_respond_to testresult_class, :failures
 end

 # returns all instances of the TestResult class
 def testresult_instances
   instances_of Test::Unit::TestResult
 end

 # this test succeeds, so there seems to be exactly one instance of
 # the TestResult class
 def test_find_instance
   assert_equal 1, testresult_instances.size
 end

 # this test doesn't succeed, so the instances of the TestResult
 # class don't have a method named "failures", either. Look at the
 # error message to find the instance variables of the TestResult
 # instance
 def test_access_to_failures_via_instance
   testresult_instance = testresult_instances[ 0 ]
   assert_respond_to testresult_instance, :failures
 end

 # without reading the Test::Unit source code, the only way to get 

at
# the current list of failures I found is to look for the one
# instance of TestResult and to access its “@failures” instance
# variable
def test_access_to_failures_instance_variable
testresult_instance = testresult_instances[ 0 ]
failures = testresult_instance.instance_variable_get :@failures
assert_not_nil failures
end

end

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

Regards,
Pit


#3

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.