[英]RSpec Scenario Outlines: Multiple Test Cases
What's the best way to test a bunch of different test cases with RSpec? 使用RSpec测试一堆不同测试用例的最佳方法是什么?
For example, given string-additions.rb : 例如,给定string-additions.rb :
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
when I run rspec string-additions.rb --color --format doc
, I get: 当我运行
rspec string-additions.rb --color --format doc
,我得到:
String
#reverse_words
should == hello
should == world hello
should == run pop & bob
However, I'd like to get sensible output, like this: 但是,我希望获得合理的输出,如下所示:
String
#reverse_words
"hello" => "hello"
"hello world" => "world hello"
"bob & pop run" => "run pop & bob"
And, I'd like to DRY up my specs a bit. 而且,我想稍微干掉我的规格。 Does RSpec provide a template for DRYing up this sort of multiple-case testing?
RSpec是否提供了用于干预这种多案例测试的模板? Something similar to Cucumber scenario outlines ?
类似黄瓜情景的概述 ?
Note: This question is similar to Is there an equivalent in RSpec to Cucumber's “Scenarios” or am I using RSpec the wrong way? 注意:这个问题类似于RSpec与Cucumber的“场景”中的等价物,还是我使用RSpec的方式错误? but provides an example that should be tested with RSpec rather than Cucumber.
但提供了一个应该用RSpec而不是Cucumber测试的例子。
After reading Elisabeth Hendrickson's Adventures with Auto-Generated Tests and RSpec , I came up with this solution: 在阅读了Elisabeth Hendrickson的自动生成测试和RSpec冒险之后,我想出了这个解决方案:
describe String do
describe "#reverse_words" do
strings = {
"hello" => "hello",
"hello world" => "world hello",
"bob & pop run" => "run pop & bob"
}
strings.each do |k, v|
specify "\"#{k}\" => \"#{v}\"" do
k.reverse_words.should eq(v)
end
end
end
end
This gives the output I want, but it'd be nicer if RSpec had a template to make things even DRYer. 这给出了我想要的输出,但是如果RSpec有一个模板可以让事情变得更干净,那就更好了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.