Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

はじめに

毎回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    

参考資料

https://github.com/carrierwaveuploader/carrierwave/wiki

hsu
nijibox
ニジボックスの開発は、社内のUI/UXデザインチームと連携をとりながらワンストップで行う開発支援サービスです。Reactを始めPHP(Laravel)・Ruby on Rails、Swift・Kotlinを使った開発実績も多く、バックエンドからアプリまで幅広く対応しています。Twitterで情報発信しています: https://twitter.com/nijibox_jp
https://nijibox.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした