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
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以下结果因“拯救”程序的存在:
Started .Error: undefined method `empty?' for nil:NilClass F Finished in 0.01 seconds. 1) Failure: test_check:18 exception expected but none was thrown. 1 tests, 2 assertions, 1 failures, 0 errors
当我不能从我的代码中删除'rescue'时,如何为这种情况编写测试用例?
评论'救援'出来对我来说产生了有利的结果。
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
Started .. Finished in 0.0 seconds. 1 tests, 2 assertions, 0 failures, 0 errors
我认为测试方法的实际行为应该是它。其实我试图测试传递给该方法的每个可能的错误输入,看看它是否可以处理它。 对不起,新手在红宝石中,只是把我的手放在tdd上。 :) – 2009-08-12 05:38:42
测试所有错误的输入无疑是正确的。这就是你如何检查错误输入的正确行为。 – djna 2009-08-12 05:58:43