它要求实现类Tester,它接收一个类并运行以test开头的所有方法。
class AssertionFailed < Exception
end
class TestCase
def setUp
end
def tierDown
end
def assertTrue(expresion)
if not expresion then
raise AssertionFailed.new(expresion.to_s + " is not true")
end
end
def assertEquals(result, expected)
if result != expected
raise AssertionFailed.new(result.to_s + " is not equal to " + expected.to_s)
end
end
end
class IntegerTest < TestCase
def setUp
@number = 1
end
def testSum
assertTrue(1 + @number == 2)
@number += 1
end
def testSub
assertTrue(2 - @number == @number)
end
def testMulByZero
assertEquals(@number*0, 1)
end
def testAddByZero
assertEquals(@number + 0, @number + 1)
end
end
Tester.test(IntegerTest)
示例:
Tester.test(IntegerTest)
[*] testMulByZero failed: 0 is not equals to 1
[*] testAddByZero failed: 1 is not equals to 2
帮助:Iterable模块的grep方法接收正则表达式,并返回all 与该表达式匹配的元素。对于练习,请使用grep(\ test *) 收集方法以获得所寻求的方法。
答案 0 :(得分:0)
我终于得到了source和one的答案 我所做的是开始给出的测试,然后我创建一个数组,通过要求de tes testig他的instace开始测试,最后它是一个迭代数组要求执行每个方法,如果他们失败的断言然后告诉他们
class Tester
def self.test(testing)
tested=testing.new
tested.setUp
method_arr=testing.instance_methods.grep(/test*/)
method_arr.each do |x|
begin
tested.send(:"#{x}")
rescue AssertionFailed =>assert_fail
puts "[*]" + "#{assert_fail}"
end
end
end
端