RSpec 中是否可以有参数化规格?
如果我有一个规范,需要使用不同的值运行才能驱动真正的实现,而不是天真的实现。一个例子:
it "should return 'fizz' for multiples of three" do
@fizzbuzz.get_value(3).should == "fizz"
end
到目前为止我还没有找到任何方法将 3 作为参数传入。下面的规范解决了我的问题,但我想知道这是否是推荐的方法,或者是否有其他更好的方法。
it "should return 'fizz' for multiples of three" do
[3, 6].each{|number| @fizzbuzz.get_value(number).should == "fizz" }
end
我不喜欢这个,因为它使用循环,不可读,并且在运行时仅显示为一个规范,我宁愿让它显示为两个不同的测试。
If I have a spec that needs to be run with different values to have it drive a real implementation and not a naive one. An example:
it "should return 'fizz' for multiples of three" do
@fizzbuzz.get_value(3).should == "fizz"
end
So far I haven't found any way to pass 3 in as a parameter. The spec below solves my problem but I'm wondering if it's the recommended way to do it or if there is any other, better way.
it "should return 'fizz' for multiples of three" do
[3, 6].each{|number| @fizzbuzz.get_value(number).should == "fizz" }
end
I don't like this because it uses loops, it's not readable and it only shows up as one spec when run, I would rather have it show up as two different tests.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要生成单独的测试,您可以执行以下操作:
由于规范文件可以简单地作为 ruby 脚本运行,因此您可以使用所有标准 ruby 构造来动态生成测试。
更好的是,您可以使用
Numeric#step
轻松生成一定范围的测试,例如[3, 6, 9, 12, 15, 18]
:To generate separate tests you could do:
Because spec files are simple run as ruby scripts you can use all the standard ruby constructs to generate tests on the fly.
Even better you can use
Numeric#step
to easily generate a certain range of tests, e.g. for[3, 6, 9, 12, 15, 18]
: