ruby-on-rails - rspec/cucumber 运行后在哪里清理 cloudinary 文件上传

标签 ruby-on-rails rspec cucumber cloudinary

我使用fixture_file_upload在我的FactoryGirl测试文件上传的方法。问题是,清理数据库后,所有这些上传的文件仍保留在 Cloudinary 上。 。

我一直在使用Cloudinary::Api.delete_resources使用 rake 任务来摆脱它们,但我宁愿在 DatabaseCleaner 之前立即清理它们删除所有相关的公共(public) ID。

我应该在哪里干扰DatabaseCleanerCloudinary 中删除这些文件?

最佳答案

根据 @phoet 的输入,并考虑到 cloudinary 限制了您一天可以执行的 API 调用数量以及单次调用中可以清理的图像数量,我创建了一个类

class CleanupCloudinary
  @@public_ids = []

  def self.add_public_ids
    Attachinary::File.all.each do |image|
      @@public_ids << image.public_id

      clean if @@public_ids.count == 100
    end
  end

  def self.clean
    Cloudinary::Api.delete_resources(@@public_ids) if @@public_ids.count > 0

    @@public_ids = []
  end
end

我的使用方式如下:在我的工厂女孩​​文件中,我在创建广告后立即调用以添加任何 public_ids:

after(:build, :create) do 
  CleanupCloudinary.add_public_ids
end

在 env.rb 中,我添加了

at_exit do
  CleanupCloudinary.clean
end

以及spec_helper.rb

config.after(:suite) do
  CleanupCloudinary.clean
end

这会导致在测试期间,每 100 个云图像进行清理,并在测试后清理剩余的图像

关于ruby-on-rails - rspec/cucumber 运行后在哪里清理 cloudinary 文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19208892/

相关文章:

ruby-on-rails - 使用 ActiveJob,perform_now 的结果似乎与 perform_later 不同

mysql - Rails - 将模型与用户相关联

ruby-on-rails - rspec 测试 Controller 后将我的参数从符号更改为字符串并破坏我的测试

ruby-on-rails - cucumber 中的http基本身份验证测试

ruby-on-rails - capybara 如何使用 href 模式测试多个链接

ruby-on-rails - rspec-sidekiq:如何用另一个类方法测试 "within_sidekiq_retries_exhausted_block"

ruby-on-rails - Pluck 和 ids 给出非唯一元素的数组

rspec - 为每次测试开始一个新的 capybara session

ruby - 类型错误 : can't define singleton (RSpec)

angularjs - 如何在 Protractor 中使用 Jasmine 和 CucumberJS