关于 HermanD 的回答,既然这是 Ruby!,您也可以直接在类中执行此操作以创建独特的测试方法:
class MyObjectTest < Test::Unit::TestCase
[
{:param => 1, :expected => 'whatever is expected'},
{:param => 2, :expected => 'whatever is expected'},
{:param => 3, :expected => 'whatever is expected'},
{:param => 4, :expected => 'whatever is expected'},
{:param => 5, :expected => 'whatever is expected'},
{:param => 6, :expected => 'whatever is expected'}
].each do |test_case|
define_method :"test_using_#{test_case[:param]}_should_return_#{params[:expected].underscore}" do
assert_equal test_case[:expected], MyObject.new.do_something_with(test_case[:param])
end
end
end
使用 Rspec(或 Shoulda 的)句子之类的语言感觉更自然:
describe MyObject do
[
{:param => 1, :expected => 'whatever is expected'},
{:param => 2, :expected => 'whatever is expected'},
{:param => 3, :expected => 'whatever is expected'},
{:param => 4, :expected => 'whatever is expected'},
{:param => 5, :expected => 'whatever is expected'},
{:param => 6, :expected => 'whatever is expected'}
].each do |test_case|
it "should return #{test_case[:expected]} when using #{test_case[:param]}" do
MyObject.new.do_something_with(test_case[:param]).should == test_case[:expected]
end
end
end