RSPEC、DRY 使用数百个用例进行测试

发布于 2024-10-23 17:30:57 字数 448 浏览 1 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独孤求败 2024-10-30 17:30:57

您可以将 it 测试块放入一个循环中,并运行您想要的每个 for 的所有时间的集合(在下面的示例中,这些项目位于 search_items集合)

require 'spec_helper'

describe IncomingMailsController do

  include Devise::TestHelpers

  search_items.each do |item|
    it "should find the correct text (#{item}) in the sample" do
      sample_text = '100s of these'
      target_text = item
      .... ALL Kinds of stuff to process (30+ lines)
      thread.content.should == 'find me'
    end
  end

end

更新

我在这个问题中的另一个答案的评论中看到您有一个包含 100 多个示例文件的目录,您可以使用 Dir#glob。然后,您可以在循环中使用它来生成 it 测试用例。

更新 2

  Dir.glob('/path/to/files/*.txt').each do |file_name|
    it "should find the correct text in the sample #{file_name}" do
      file_content = File.open(file_name, "rb")
      sample_text = '100s of these'
      target_text = 'fine me'
      .... ALL Kinds of stuff to process (30+ lines)
      thread.content.should == 'find me'
    end
  end

您可能需要使用 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 the search_items collection)

require 'spec_helper'

describe IncomingMailsController do

  include Devise::TestHelpers

  search_items.each do |item|
    it "should find the correct text (#{item}) in the sample" do
      sample_text = '100s of these'
      target_text = item
      .... ALL Kinds of stuff to process (30+ lines)
      thread.content.should == 'find me'
    end
  end

end

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

  Dir.glob('/path/to/files/*.txt').each do |file_name|
    it "should find the correct text in the sample #{file_name}" do
      file_content = File.open(file_name, "rb")
      sample_text = '100s of these'
      target_text = 'fine me'
      .... ALL Kinds of stuff to process (30+ lines)
      thread.content.should == 'find me'
    end
  end

You might have to play with the full path of file_name but that should get you some of the way.

挽袖吟 2024-10-30 17:30:57

不确定这是否是您正在寻找的,但是怎么样:

['xxxxxx', 'yyyyyyy', 'zzzzzzzz'].each do |thing|
  it "finds #{thing} in the sample do
    # and so on...
  end
end

当然,您可能会选择除数组之外的其他东西来存储测试字符串,但这应该可以帮助您开始。

Not sure if this is what you're looking for, but how about:

['xxxxxx', 'yyyyyyy', 'zzzzzzzz'].each do |thing|
  it "finds #{thing} in the sample do
    # and so on...
  end
end

Naturally, you might opt for something other than an array for storing your test strings, but this should get you started.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文