RSPEC、DRY 使用数百个用例进行测试
在 rspec 中,我有一个看起来像这样的规范:
require 'spec_helper'
describe IncomingMailsController do
include Devise::TestHelpers
it "should find the correct text in the sample" do
sample_text = '100s of these'
target_text = 'find me'
.... ALL Kinds of stuff to process (30+ lines)
thread.content.should == 'find me'
end
end
是否有一种使用 rspec 的方法,允许我以某种方式在一个块中使用所有逻辑?然后创建 100 个“xxxxx”,并传递一个变量作为示例文本?
谢谢
in rspec, I have a spec that looks something like this:
require 'spec_helper'
describe IncomingMailsController do
include Devise::TestHelpers
it "should find the correct text in the sample" do
sample_text = '100s of these'
target_text = 'find me'
.... ALL Kinds of stuff to process (30+ lines)
thread.content.should == 'find me'
end
end
Is there a way with rspec, to allow me to somehow use all that logic in one block? And then create 100s of it "xxxxx" do, and pass that a variable being the sample text?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将
it
测试块放入一个循环中,并运行您想要的每个 for 的所有时间的集合(在下面的示例中,这些项目位于search_items
集合)更新
我在这个问题中的另一个答案的评论中看到您有一个包含 100 多个示例文件的目录,您可以使用 Dir#glob。然后,您可以在循环中使用它来生成
it
测试用例。更新 2
您可能需要使用 file_name 的完整路径,但这应该可以帮助您解决问题。
You can put the
it
test blocks inside a loop, and run through a collection of all the times you want to each for (in the example below those items are in thesearch_items
collection)UPDATE
I saw on a comment to another answer in this threase that you have a directory with 100+ files of samples, you can get the list of filenames using Dir#glob. You can then use that in the loop to generate your
it
test cases.UPDATE 2
You might have to play with the full path of file_name but that should get you some of the way.
不确定这是否是您正在寻找的,但是怎么样:
当然,您可能会选择除数组之外的其他东西来存储测试字符串,但这应该可以帮助您开始。
Not sure if this is what you're looking for, but how about:
Naturally, you might opt for something other than an array for storing your test strings, but this should get you started.