LoginSignup
4
4

More than 5 years have passed since last update.

Rspecのcarrierwaveを実行した後のファイルを削除

Posted at

はじめに

毎回Rspecを実行した後画像は増えてきて困ることで、削除したい手順です。

手順

1.Rspec実行してアップロードされた画像を異なるディレクトリに配置

qiita.rb
class MyFileUploader < CarrierWave::Uploader::Base
def store_dir
  if Rails.env.test?
    "MyFile/#{Rails.env}/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  else
    "MyFile/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
end

2.spec/rails_helperで下記のような、after(:all)でテストの最後に実行されます

qiita.rb
config.after(:all) do
  if Rails.env.test? 
    FileUtils.rm_rf(Rails.root + "public/MyFile/#{Rails.env}/")
   end
end    

参考資料

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4