我需要一个解决方案来清理我用RSpec测试时上传的附件文件

时间:2017-06-15 18:05:42

标签: ruby rspec carrierwave factory-bot ruby-on-rails-4.2

大家好我正在使用RSpec进行测试,我对使用工厂的上传文件进行了一些测试。 我设置了数据库清理器,但是,当我在公共文件夹上创建一个树时,它每次都创建一个新文件,我想每次运行rspec命令时都清理它。

tree public
public
├── 404.html
├── 422.html
├── 500.html
├── attachment
├── favicon.ico
├── robots.txt
└── uploads
    ├── attachment
    │   └── file
    │       ├── 1
    │       │   ├── photo.jpg
    │       │   ├── photo02.jpg
    │       │   └── photo03.jpg
    │       ├── 10
    │       │   └── photo03.jpg
    │       ├── 11
    │       │   └── photo.jpg
    │       ├── 12
    │       │   └── photo02.jpg
    │       ├── 13
    │       │   └── photo.jpg
    │       ├── 14
    │       │   └── photo02.jpg
    │       ├── 15
    │       │   └── photo03.jpg
    │       ├── 16
    │       │   └── photo.jpg
    │       ├── 17
    │       │   └── photo02.jpg
    │       ├── 18
    │       │   └── photo.jpg
    │       ├── 19
    │       │   └── photo02.jpg
    │       ├── 2
    │       │   ├── photo.jpg
    │       │   └── photo02.jpg
    │       ├── 20
    │       │   └── photo03.jpg
    │       ├── 21
    │       │   └── photo.jpg
    │       ├── 22
    │       │   └── photo02.jpg
    │       ├── 23
    │       │   └── photo.jpg
    │       ├── 24
    │       │   └── photo02.jpg
    │       ├── 25
    │       │   └── photo03.jpg
    │       ├── 26
    │       │   └── photo.jpg
    │       ├── 27
    │       │   └── photo02.jpg
    │       ├── 28
    │       │   └── photo.jpg
    │       ├── 29
    │       │   └── photo02.jpg
    │       ├── 3
    │       │   ├── photo.jpg
    │       │   └── photo03.jpg
    │       ├── 30
    │       │   └── photo.jpg
    │       ├── 31
    │       │   └── photo02.jpg
    │       ├── 32
    │       │   └── photo.jpg
    │       ├── 33
    │       │   └── photo02.jpg
    │       ├── 34
    │       │   └── photo.jpg
    │       ├── 35
    │       │   └── photo02.jpg
    │       ├── 36
    │       │   └── photo.jpg
    │       ├── 37
    │       │   └── photo02.jpg
    │       ├── 38
    │       │   └── photo.jpg
    │       ├── 39
    │       │   └── photo02.jpg
    │       ├── 4
    │       │   └── photo02.jpg
    │       ├── 40
    │       │   └── photo.jpg
    │       ├── 41
    │       │   └── photo02.jpg
    │       ├── 42
    │       │   └── photo.jpg
    │       ├── 43
    │       │   └── photo02.jpg
    │       ├── 44
    │       │   └── photo.jpg
    │       ├── 45
    │       │   └── photo02.jpg
    │       ├── 46
    │       │   └── photo.jpg
    │       ├── 47
    │       │   └── photo02.jpg
    │       ├── 48
    │       │   └── photo.jpg
    │       ├── 49
    │       │   └── photo02.jpg
    │       ├── 5
    │       │   └── photo03.jpg
    │       ├── 50
    │       │   └── photo.jpg
    │       ├── 51
    │       │   └── photo02.jpg
    │       ├── 52
    │       │   └── photo.jpg
    │       ├── 53
    │       │   └── photo02.jpg
    │       ├── 54
    │       │   └── photo.jpg
    │       ├── 55
    │       │   └── photo02.jpg
    │       ├── 56
    │       │   └── photo.jpg
    │       ├── 57
    │       │   └── photo02.jpg
    │       ├── 58
    │       │   └── photo.jpg
    │       ├── 59
    │       │   └── photo02.jpg
    │       ├── 6
    │       │   └── photo.jpg
    │       ├── 60
    │       │   └── photo.jpg
    │       ├── 61
    │       │   └── photo02.jpg
    │       ├── 62
    │       │   └── photo.jpg
    │       ├── 63
    │       │   └── photo02.jpg
    │       ├── 64
    │       │   └── photo.jpg
    │       ├── 65
    │       │   └── photo02.jpg
    │       ├── 66
    │       │   └── photo.jpg
    │       ├── 67
    │       │   └── photo02.jpg
    │       ├── 68
    │       │   └── photo.jpg
    │       ├── 69
    │       │   └── photo02.jpg
    │       ├── 7
    │       │   └── photo02.jpg
    │       ├── 70
    │       │   └── photo.jpg
    │       ├── 71
    │       │   └── photo02.jpg
    │       ├── 72
    │       │   └── photo.jpg
    │       ├── 73
    │       │   └── photo02.jpg
    │       ├── 74
    │       │   └── photo.jpg
    │       ├── 75
    │       │   └── photo02.jpg
    │       ├── 8
    │       │   └── photo.jpg
    │       └── 9
    │           └── photo02.jpg
    ├── post
    └── tmp

因此,为了配置数据库清理器,我放入了spec / support / database_cleaner.rb:

RSpec.configure do |config|
  config.before(:suite) do
    DatabaseCleaner.strategy = :deletion
    DatabaseCleaner.clean_with(:deletion)
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end
end

我的rails_helper:

# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require "pundit/rspec"


# Add additional requires below this line. Rails is not loaded until this point!

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
 Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

# Checks for pending migration and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = false

  # RSpec Rails can automatically mix in different behaviours to your tests
  # based on their file location, for example enabling you to call `get` and
  # `post` in specs under `spec/controllers`.
  #
  # You can disable this behaviour by removing the line below, and instead
  # explicitly tag your specs with their type, e.g.:
  #
  #     RSpec.describe UsersController, :type => :controller do
  #       # ...
  #     end
  #
  # The different available types are documented in the features, such as in
  # https://relishapp.com/rspec/rspec-rails/docs
  config.infer_spec_type_from_file_location!

  # Filter lines from Rails gems in backtraces.
  config.filter_rails_from_backtrace!
  # arbitrary gems may also be filtered via:
  # config.filter_gems_from_backtrace("gem name")

  # Include Factory Girl syntax to simplify calls to factories
  config.include FactoryGirl::Syntax::Methods

  Shoulda::Matchers.configure do |config|
    config.integrate do |with|
      # Choose a test framework:
      with.test_framework :rspec
      # Or, choose the following (which implies all of the above):
      with.library :rails
    end
  end

  config.include Warden::Test::Helpers, type: :feature
  config.after(type: :feature) { Warden.test_reset! }
  config.use_transactional_fixtures = false
  config.include Devise::Test::ControllerHelpers, type: :controller
end

我的附件工厂.rb:

FactoryGirl.define do
  factory :attachment do
    transient do
      file_to_attach "spec/fixtures/photo.jpg"
    end

    file { File.open file_to_attach }
  end
end

3 个答案:

答案 0 :(得分:1)

这就是我最近在我的一个项目中的用法:

config/initializers/carrierwave.rb

if Rails.env.test?
  Dir["#{Rails.root}/app/uploaders/*.rb"].each { |file| require file }

  CarrierWave::Uploader::Base.descendants.each do |klass|
    next if klass.anonymous?
    klass.class_eval do
      def cache_dir
        "#{Rails.root}/spec/support/uploads/tmp"
      end

      def store_dir
        "#{Rails.root}/spec/support/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
      end
    end
  end
end

并在spec/spec_helper.rb

config.after(:each) do
  FileUtils.rm_rf(Dir["#{Rails.root}/spec/support/uploads"])
end

答案 1 :(得分:0)

我在网上寻求并尝试这样做,现在它正在运作。 我认为配置是我的理想选择。但如果有人看到不同意的事情,请告诉我!

我在config/initializers/carrierwave.rb

上配置
if Rails.env.test? || Rails.env.cucumber?

  CarrierWave.configure do |config|
   config.storage = :file
   config.enable_processing = false
  end

  CarrierWave::Uploader::Base.descendants.each do |klass|
   next if klass.anonymous?
   klass.class_eval do
     def cache_dir
       "#{Rails.root}/public/uploads/tmp"
     end

     def store_dir
       "#{Rails.root}/public/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
     end
   end
 end
end

CarrierWave.configure do |config|
  config.asset_host = ActionController::Base.asset_host
end

在我的spec_helper.rb中,我添加了这个:

 config.after(:each) do
   if Rails.env.test? || Rails.env.cucumber?
     FileUtils.rm_rf(Dir["#{Rails.root}/public/uploads"])
   end
  end

现在我运行rspec命令后,我的树就是这样:

tree public
public
├── 404.html
├── 422.html
├── 500.html
├── attachment
├── favicon.ico
└── robots.txt

所以它被删除了创建测试的文件,现在我发现另一个问题是在开发模式下创建的文件被删除了,每次运行测试时都会被删除,这不是很好! 如果有人能帮我解决这个新问题?我将不胜感激!

答案 2 :(得分:0)

我正在做另外的事情,我正在支持编写这个项目,现在我设置上传模式测试和开发的不同路径。所以这就是解决方案。并分享代码。我发布了这个。 感谢Daniel Friis的帮助。

配置/初始化/ carrerwave.rb

if Rails.env.test? || Rails.env.cucumber?
  CarrierWave.configure do |config|
    config.storage = :file
    config.enable_processing = false
  end

  # make sure our uploader is auto-loaded
  AttachmentUploader

  # use different dirs when testing
  CarrierWave::Uploader::Base.descendants.each do |klass|
    next if klass.anonymous?
    klass.class_eval do
      def cache_dir
        "#{Rails.root}/spec/support/uploads/tmp"
      end

      def store_dir
        "#{Rails.root}/spec/support/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
      end
    end
  end
end

CarrierWave.configure do |config|
  config.asset_host = ActionController::Base.asset_host
end

并加上spec / rails_helper:

# Clean up uploaded files
  config.after(:each) do
    if Rails.env.test? || Rails.env.cucumber?
      FileUtils.rm_rf(Dir["#{Rails.root}/spec/support/uploads"])
    end
  end