RSpec 场景概要:多个测试用例
使用 RSpec 测试一堆不同测试用例的最佳方法是什么?
例如,给定 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
当我运行 rspec string-additions.rb 时--color --format doc
,我得到:
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"
而且,我想干燥我的规格。 RSpec 是否提供了用于干燥此类多案例测试的模板?类似于 Cucumber 场景概述?
注意:这个问题类似于 RSpec 中是否有与 Cucumber 的“场景”等效的内容,或者我是否以错误的方式使用 RSpec? 但提供了一个应该使用 RSpec 进行测试的示例,而不是使用 RSpec 进行测试 黄瓜。
What's the best way to test a bunch of different test cases with RSpec?
For example, given 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:
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? 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? but provides an example that should be tested with RSpec rather than Cucumber.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
阅读Elisabeth Hendrickson 的自动生成测试的冒险之后和 RSpec,我想出了这个解决方案:
这给出了我想要的输出,但如果 RSpec 有一个模板来使事情变得更干燥,那就更好了。
After reading Elisabeth Hendrickson's Adventures with Auto-Generated Tests and RSpec, I came up with this solution:
This gives the output I want, but it'd be nicer if RSpec had a template to make things even DRYer.