How to erase test image after running Rspec test with CarrierWave

Introduction

I'm a Ruby beginner who creates a portfolio with Ruby on Rails. I think that there are many people who have implemented the image upload function in CarrierWave. I am also one of them. This gem is used for user avatar settings and image attachment when posting, and the image upload function of CarrierWave was included for confirmation in various tests.

While creating the portfolio, after running the test several times, when I happened to look at the folder where the images are saved ... ** The number of folders is 600! ?? ** **

I didn't notice it at all. The more you turn the test, the more the images included in the test case will be uploaded over and over again. Before I knew it, my folder was tight with image capacity ...

You can turn it off manually, but the tests are done with each refactoring or feature addition, which is quite annoying. So, ** I will set it to erase the image of CarrierWave created for the test ** after running the test.

environment

I am developing rails by creating a container in Docker.

Docker for mac 2.3.0.4
Ruby on Rails 5.2.2
ruby 2.4.5
CarrierWave 2.1.0

The answer is on the CarrierWave Wiki

I referred to the following page on the CarrierWave wiki How to: Cleanup after your Rspec tests

While referring to this, delete it according to the following flow. ** 1. Specify the upload folder for test (uploeader file) ** ** 2. Clear all folders with the specified path after running the rspec test **

** 1. Specify upload folder during test ** Since some cache files are also generated (during error check test cases) Specify both cache and store folders.

app/uploaders/***_uploader.rb



:
  #Specify cache folder
  def cache_dir
    if Rails.env.test?
      "uploads/#{Rails.env}/tmp/#{mounted_as}/#{model.id}"
    else
      "uploads/tmp/#{mounted_as}/#{model.id}"
    end
  end

 #Specify the folder of store
  def store_dir
    if Rails.env.test?
      "uploads/#{Rails.env}/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    else
      "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    end
  end
:

** 2. Clear all folders in the specified path after running the rspec test ** Write the path to delete the cache and store folder specified above at once.

spec/spec_helper.rb


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

This will automatically clear the test folder every time you run the rspec test.

in conclusion

I've run the test many times and now I don't have a lot of folders or tight space, Impression that the processing speed of rspec has slowed down a little because the process of erasing has entered the test. This can't be helped, but it's a good idea to tell if you really need image uploads during the test, and if not, consider creating factories without images. I would also like to review the test cases.

Recommended Posts

How to erase test image after running Rspec test with CarrierWave
Image upload using CarrierWave ~ Rspec test ~
How to test a private method with RSpec for yourself
[RSpec] How to write test code
How to crop an image with libGDX
How to test private scope with JUnit
How to write an RSpec controller test
[Java] How to test for null with JUnit
I want to test Action Cable with RSpec test
How to test interrupts during Thread.sleep with JUnit
How to use "sign_in" in integration test (RSpec)
How to give your image to someone with docker
How to redirect after user login with Spring-security
Introduction to RSpec 1. Test, RSpec
[Rails] Test with RSpec
Test Nokogiri with Rspec.
How to unit test with JVM with source using RxAndroid
[Rails] How to deal with URL changes after render
Introduction to RSpec 4. Create test data with Factory Bot
[RSpec] How to test error messages set by Shoulda-Matchers
How to run only specific files with gem's rake test
Test Active Strage with RSpec
How to number (number) with html.erb
How to update with activerecord-import
Test GraphQL resolver with rspec
How to deal with the event that Committee :: InvalidRequest occurs in committee during Rspec file upload test
[Rails / RSpec] How to deal with element has zero size error
[RSpec on Rails] How to write test code for beginners by beginners
How to implement UI automated test using image comparison in Selenium
How to scroll horizontally with ScrollView
How to unit test Spring AOP
How to get started with slim
Test with RSpec + Capybara + selenium + chromedriver
How to enclose any character with "~"
How to use mssql-tools with alpine
How to get along with Rails
Copy and paste test with RSpec
How to distinguish ubuntu cloud image
Let's unit test with [rails] Rspec!
How to start Camunda with Docker
How to test private methods with arrays or variadic arguments in JUnit
How to adjustTextPosition with iOS Keyboard Extension
How to share files with Docker Toolbox
How to compile Java with VsCode & Ant
[Ruby on Rails] View test with RSpec
[Ruby on Rails] How to use CarrierWave
[Rails] How to upload images using Carrierwave
[Java] How to compare with equals method
How to filter JUnit Test in Gradle
How to blur an image (super easy)
[Android] How to deal with dark themes
How to use BootStrap with Play Framework
[Rails] How to use rails console with docker
How to switch thumbnail images with JavaScript
How to do API-based control with cancancan
[Ruby on Rails] Controller test with RSpec
How to achieve file download with Feign
How to update related models with accepts_nested_attributes_for
How to set JAVA_HOME with Maven appassembler-maven-plugin
How to implement TextInputLayout with validation function
How to handle sign-in errors with devise