Delegating testing to another class

K

Ken Coar

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
 
K

Ken Coar

Found a solution:

Ken said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top