【发布时间】:2009-08-11 05:56:31
【问题描述】:
我有以下代码和相应的测试用例:
class XXX
attr_accessor :source
def check
begin
raise ArgumentError, "No source specified." \
unless @source.empty? != true
puts "Passed"
rescue
print "Error: ",$!, "\n"
end
end
end
class TestXXX < Test::Unit::TestCase
def setup
@x = XXX.new
end
def test_check
assert_nil(@x.source)
assert_raise(NoMethodError) { @x.check }
end
end
由于存在“救援”例程,运行测试用例将在 assert_raise 上产生以下结果:
开始 .错误:未定义的方法“空?”对于零:NilClass F 在 0.01 秒内完成。 1) 失败: test_check:18 预期异常,但没有抛出任何异常。 1 次测试,2 次断言,1 次失败,0 次错误当我无法从代码中删除“救援”时,如何为这种情况编写测试用例?
评论“救援”对我来说是一个有利的结果。
class XXX
attr_accessor :source
def check
begin
raise ArgumentError, "No source specified." \
unless @source.empty? != true
puts "Passed"
#rescue
# print "Error: ",$!, "\n"
end
end
end
开始
..
在 0.0 秒内完成。
1 次测试,2 次断言,0 次失败,0 次错误
【问题讨论】:
标签: ruby unit-testing exception-handling