Delegating testing to another class

This is probably funky… :slight_smile:

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

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.