# Delegating testing to another class

**URL:** <https://www.ruby-forum.com/t/delegating-testing-to-another-class/183430>\
**Category:** Ruby\
**Created:** [February 13, 2010, 8:27pm UTC](https://www.ruby-forum.com/t/delegating-testing-to-another-class/183430 "2010-02-13T20:27:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lqqkout4elfy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ebca7d/32.png) [@lqqkout4elfy](https://www.ruby-forum.com/u/lqqkout4elfy)\
**Post date:** [February 13, 2010, 8:27pm UTC](https://www.ruby-forum.com/t/delegating-testing-to-another-class/183430/1 "2010-02-13T20:27:42Z")

</div>

This is probably funky… 🙂

I’m writing some tests with Test::Unit::TestCase, and I want them to be  
data-driven. I want to abstract the assertions to another class (call  
it TestIt) more tightly tied to the data.

So far, I either get NoMethodError exceptions because TestIt isn’t  
subclassed from Test::Unit::TestCase nor mixes in any of those modules,  
or else (if I _do_ subclass or mixin) the assertions test properly but  
don’t get counted in the total tally displayed because they’re not being  
performed by the main Test::Unit::TestCase subclass.

It seems as though I might be able to address the first somehow with  
bindings and closure, but I don’t know how.

In the meantime, I’m trying another approach, but this is an interesting  
scenario I’d like to explore more.

Here’s a stripped-down sample:

Each TestIt object includes the data to be tested.

class TestMyStuff \< Test::Unit::TestCase  
def test1()  
TestVals.each do |t1| # TestVals is an array of TestIt objects  
t1.test do |t2|  
true  
end  
end  
end  
end

class TestIt  
def test(&block)  
assert(“Descriptive message”) do  
block.call  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![lqqkout4elfy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ebca7d/32.png) [@lqqkout4elfy](https://www.ruby-forum.com/u/lqqkout4elfy)\
**Post date:** [February 13, 2010, 9:11pm UTC](https://www.ruby-forum.com/t/delegating-testing-to-another-class/183430/2 "2010-02-13T21:11:44Z")

</div>

Found a solution:

Ken C. wrote:

> class TestMyStuff \< Test::Unit::TestCase  
> def test1()  
> TestVals.each do |t1| # TestVals is an array of TestIt objects

```
    t1.tCase = self

```

> ```
> t1.test do |t2|
> true
> end
> end
> 
> ```
> 
> end  
> end
> 
> class TestIt  
> def test(&block)

```
  @tCase.assert("Descriptive message") do

```

> ```
> block.call
> end
> 
> ```
> 
> end  
> end

Not exactly elegant, but by passing the caller I can access its assert\*  
methods explicitly rather than implicitly.
