使用 RSpec 测试一堆不同测试用例的最佳方法是什么?
例如,给定字符串添加.rb https://gist.github.com/899520:
require 'rspec'
class String
if method_defined? :reverse_words
raise "String#reverse_words is already defined"
end
def reverse_words
split(' ').reverse!.join(' ')
end
end
describe String do
describe "#reverse_words" do
specify { "hello".reverse_words.should eq("hello") }
specify { "hello world".reverse_words.should eq("world hello") }
specify { "bob & pop run".reverse_words.should eq("run pop & bob") }
end
end
当我跑步时rspec string-additions.rb --color --format doc
, I get:
String
#reverse_words
should == hello
should == world hello
should == run pop & bob
但是,我想得到合理的输出,如下所示:
String
#reverse_words
"hello" => "hello"
"hello world" => "world hello"
"bob & pop run" => "run pop & bob"
而且,我愿意DRY http://en.wikipedia.org/wiki/Don%27t_repeat_yourself稍微提高一下我的规格。 RSpec 是否提供了用于干燥此类多案例测试的模板?类似的东西黄瓜情景概述 https://github.com/aslakhellesoy/cucumber/wiki/scenario-outlines?
注意:这个问题类似于RSpec 中是否有与 Cucumber 的“场景”等效的内容,或者我是否以错误的方式使用 RSpec? https://stackoverflow.com/questions/4679875/is-there-an-equivalent-in-rspec-to-cucumbers-scenarios-or-am-i-using-rspec-the但提供了一个应该使用 RSpec 而不是 Cucumber 进行测试的示例。