Reputation: 2763
Hello guys I'm using RSpec for testing and I have some test for upload files using factories. I set up the database cleaner but, when I do a tree on the public folder it's creating a new file everytime and I want to clean this everytime I run the rspec command.
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
So for configure the database cleaner I put in the 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
my 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
my factory of 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
Upvotes: 0
Views: 1455
Reputation: 2763
I was doing another things and I'm backing to code this project and now I setup for upload on different paths for the mode test and dev. So this was the solution. And for share the code. I'm posting this. And Thank's Daniel Friis for the help.
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
and put on 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
Upvotes: 0
Reputation: 484
This is how I went about it in a recent project of mine:
In 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
and in spec/spec_helper.rb
config.after(:each) do
FileUtils.rm_rf(Dir["#{Rails.root}/spec/support/uploads"])
end
Upvotes: 2
Reputation: 2763
I was seeking on the web and try to do this and it's working right now. I think the configuration is the ideal in my case. But if anyone see something that not agree please let me know!
I config on 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
And in my spec_helper.rb I add this:
config.after(:each) do
if Rails.env.test? || Rails.env.cucumber?
FileUtils.rm_rf(Dir["#{Rails.root}/public/uploads"])
end
end
Now after I run the rspec command my tree on public is this:
tree public
public
├── 404.html
├── 422.html
├── 500.html
├── attachment
├── favicon.ico
└── robots.txt
So it was removed the files who was created testing and now I notice another problem the files who was created in development mode was deleted and will be deleted every time I run the tests and this is no so good!! If anyone can help me with this new problem? I will appreciate!
Upvotes: 1