# Asserts in aggregate objects called by a Test::Unit::TestCase

**URL:** <https://rubytalk.org/t/asserts-in-aggregate-objects-called-by-a-test-unit-testcase/27334>\
**Category:** ruby-talk\
**Created:** [10 May 2006 21:25 UTC](https://rubytalk.org/t/asserts-in-aggregate-objects-called-by-a-test-unit-testcase/27334 "2006-05-10T21:25:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Tanner](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@Andrew\_Tanner](https://rubytalk.org/u/Andrew_Tanner)\
**Post date:** [10 May 2006 21:25 UTC](https://rubytalk.org/t/asserts-in-aggregate-objects-called-by-a-test-unit-testcase/27334/1 "2006-05-10T21:25:20Z")

</div>

Thanks for the response Eric. I looked up add\_assert but it didn't  
really help that much since the aggregate object (SubAssertObj) is in a  
diff context then the original TestStuff testcase.

However I did do something similar to your other suggestion. I threw the  
validation code in a module and I'm now include'ing in my individual  
testcases as needed.

Thanks again.

> **···**
>
> -----Original Message-----  
> From: Eric Hodel [mailto:drbrain@segment7.net]  
> Sent: Wednesday, May 10, 2006 10:46 AM  
> To: ruby-talk ML  
> Subject: Re: Asserts in aggregate objects called by a  
> Test::Unit::TestCase
> 
> On May 10, 2006, at 10:31 AM, Andrew Tanner wrote:
> 
> > Howdy Rubyists,
> > 
> > I'm building a framework for automating tests so I've been sinking my  
> > teeth into Test::Unit pretty heavily. Right now I'm trying to  
> > automagically do a bunch of asserts in a sub object called by a  
> > Test::Unit::TestCase subclass similar to something like this...
> > 
> > require "test/unit"
> > 
> > class TestStuff \< Test::Unit::TestCase
> > 
> > &nbsp;&nbsp;&nbsp;def test\_stuff  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;obj = SubAssertObj.new  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;obj.verify\_stuff("please\_work")  
> > &nbsp;&nbsp;&nbsp;end
> > 
> > end
> > 
> > class SubAssertObj
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;import Test::Unit::Assertions
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;def verify\_stuff(str)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal("please\_work", str)  
> > &nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > end
> > 
> > Now if the assert\_equal fails it throws the  
> > Test::Unit::AssertionFailedError like expected and it reports it very  
> > nicely in the output. However if the assert is successful in the  
> > output  
> > from Test::Unit I get
> > 
> > 1 tests, 0 assertions, 0 failures, 0 errors
> 
> See add\_assertion in Test::Unit::TestCase and Test::Unit::Assertions
> 
> > How can I get the assertions in the sub object to be reflected in the  
> > report from Test::Unit?
> 
> Why not write it like this:
> 
> class TestStuff \< Test::Unit::TestCase
> 
> &nbsp;&nbsp;&nbsp;def test\_stuff  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;util\_verify\_stuff 'please work'  
> &nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;def util\_verify\_stuff(str)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal 'please work', str  
> &nbsp;&nbsp;&nbsp;end
> 
> end
> 
> And then use subclassing to reuse common functionality?
> 
> --  
> Eric Hodel - drbrain@segment7.net - [http://blog.segment7.net](http://blog.segment7.net)  
> This implementation is HODEL-HASH-9600 compliant
> 
> [http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)
