【发布时间】:2017-06-10 22:29:22
【问题描述】:
它要求实现 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方法接收到一个正则表达式,返回所有 与该表达式匹配的元素。对于练习,使用 grep (\ test * ) on 获取方法的集合。
【问题讨论】:
-
JFYI,像
testAddByZero这样的方法名称不符合公认的命名约定。命名的惯用方式是test_add_by_zero。一般来说,camelCase只是不用于 ruby。