我需要一个解决方案来清理我使用 RSpec 测试时上传的附件文件
I need a solution for clean the attachment files uploaded when I test with RSpec
大家好,我正在使用 RSpec 进行测试,我对使用工厂上传文件进行了一些测试。
我设置了数据库清理器,但是,当我在 public 文件夹上创建一个树时,它每次都会创建一个新文件,我想在每次 运行 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
我的attachments.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
我在网上搜索并尝试这样做,现在可以了。
我认为配置对我来说是理想的。但如果有人看到不同意的地方,请告诉我!
我在 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 命令之后,我在 public 上的树是这样的:
tree public
public
├── 404.html
├── 422.html
├── 500.html
├── attachment
├── favicon.ico
└── robots.txt
所以它被删除了创建测试的文件,现在我注意到另一个问题,在开发模式下创建的文件被删除,并且每次我 运行 测试时都会被删除,但事实并非如此好的!!
如果有人可以帮我解决这个新问题?我会感激的!
我在最近的一个项目中就是这样做的:
在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
我正在做另一件事,我正在支持编写此项目的代码,现在我为模式测试和开发设置了不同路径的上传。所以这就是解决方案。并分享代码。我正在发布这个。
还要感谢 Daniel Friis 的帮助。
config/initialize/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
大家好,我正在使用 RSpec 进行测试,我对使用工厂上传文件进行了一些测试。 我设置了数据库清理器,但是,当我在 public 文件夹上创建一个树时,它每次都会创建一个新文件,我想在每次 运行 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
我的attachments.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
我在网上搜索并尝试这样做,现在可以了。 我认为配置对我来说是理想的。但如果有人看到不同意的地方,请告诉我!
我在 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 命令之后,我在 public 上的树是这样的:
tree public
public
├── 404.html
├── 422.html
├── 500.html
├── attachment
├── favicon.ico
└── robots.txt
所以它被删除了创建测试的文件,现在我注意到另一个问题,在开发模式下创建的文件被删除,并且每次我 运行 测试时都会被删除,但事实并非如此好的!! 如果有人可以帮我解决这个新问题?我会感激的!
我在最近的一个项目中就是这样做的:
在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
我正在做另一件事,我正在支持编写此项目的代码,现在我为模式测试和开发设置了不同路径的上传。所以这就是解决方案。并分享代码。我正在发布这个。 还要感谢 Daniel Friis 的帮助。
config/initialize/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