Unit tests under Active Storage deployment

First of all, what is Active Storage?

A function for uploading files If you use this, you can easily create an image posting function etc. with a form. You can also upload files to cloud storage services It's easy to do.

In my case, I was creating an application that has a function to upload images, so I had introduced Active Storage. As a feature, there is no need to prepare a column for images. This time, with this feature, there were a lot of errors ...

The test of the main subject

The cause of this error is ① FactoryBot has created dummy image data ② In spec.rb, the fixture_file_upload method was not used

The above two were the main causes of the error.

Especially for (2), I couldn't reach it because I was just investigating the misplacement ..


Then from ①.

For example, if you have a model / table called product

ʻIf you are not using Active Storage` Image column (column for image) required

ʻIf you are using Active Storage` No need for image column (column for image)

That's right.

If you review the description of FactoryBot based on the above

ʻIf you are not using Active Storage`

products.rb


FactoryBot.define do
  factory :product do
    image             { 'test_image.jpg' }
    name              { 'Toner' }
    explanation       { 'It's a good lotion' }
    price             { '2000' }
    user
  end
end

ʻIf you are using Active Storage`

products.rb


FactoryBot.define do
  factory :product do
    name              { 'Toner' }
    explanation       { 'It's a good lotion' }
    price             { '2000' }
    user
  end
end

It's natural that this happens. Because under Active Storage environment, you don't need the image column. (I didn't notice at all ..)

This solved part (1).


Next, turn to ②.

fixture_file_upload method

fixture_file_upload method

fixture_file_upload method

I said it three times because it's important. This method is provided by RSpec.

Spec.rb when the test did not pass at all ↓↓↓↓

product_spec.rb


require 'rails_helper'


describe Product do

  describe '#create' do
    before do
      @product = build(:product)
    end


    
    # 1.Can be registered if the image exists
    it 'is valid with an image' do
      expect(@product).to be_valid
    end

    # 2.Cannot be registered if name is empty
    it 'is invalid without a name' do
      @product.name = nil
      @product.valid?
      expect(@product.errors[:name]).to include('Please enter')
    end

                        .
                        .
                        .
                        .
                        .

  end
end

Modified spec.rb ↓↓↓↓

product_spec.rb


require 'rails_helper'


describe Product do

  describe '#create' do
    before do
      @product = build(:item)
      @product.image = fixture_file_upload("/files/test_image.jpg ")
    end


    
    # 1.Can be registered if the image exists
    it 'is valid with an image' do
      expect(@product).to be_valid
    end

    # 2.Cannot be registered if name is empty
    it 'is invalid without a name' do
      @product.name = nil
      @product.valid?
      expect(@product.errors[:name]).to include('Please enter')
    end

                        .
                        .
                        .
                        .
                        .

  end
end

fixture_file_upload("/files/test_image.jpg ") What does that mean, but The test images are placed according to the directory below.

spec
  ├ factories
  ├ fixtures
  │     └ files
  │         ├ test_image.jpg
  │         └ .keep
  ├ models
  :
  :

Call back (reserve) this between before do ~ end, The image for testing is generated before the test code is executed.

With this, ② is also solved safely.


It was a cause that would never have been solved, It took a lot of time and energy to get there.

I have to improve my search power ...

I hope it will be helpful for those who do not pass the test due to the same reason.

Recommended Posts

Unit tests under Active Storage deployment
Introduce Active Storage
About Active Storage
About DelegationError (Active Storage)
Active Storage Initialization Manual
[Ralis] About unit tests
[Beginner] About Active Storage
Post videos with Active Storage
Java unit tests with Mockito