[RUBY] Image column test code when ActiveStorage is introduced

As a beginner, I was stuck with the test code when I used active storage for about an hour, so it is a memorandum.

First of all, it is assumed that the necessary active storage, factory bot, and spec have been installed.

If you haven't done so, write the following gem in your gemfile and bundle install it. Then let's install RSpec rails g rspec: install

gemfile


group :development, :test do   

  gem 'rspec-rails'         #A gem for writing and testing these 3 lines
  gem 'factory_bot_rails'   #Write these 3 lines and gem to create a test template
  gem 'pry-rails'           #Write these 3 lines, bindind.gem to run pry

  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

Terminal


% bundle install
% rails g rspec:install

First, create a file called images under the public directory Save the appropriate image data in the images (this time, prepare an image named a.png )

Now in this state public/images/a.png

a.png is the data of the appropriate image

Next, I want to refer to that data, so I will create an image column in the template in the factories directory This time I will test a model called "item" so it will be practices/items.rb

ruby:ractories/items.rb(Model name.rb)


FactoryBot.define do
  factory :item do
    name                 {"name"}
    price                {500}
    text                 {"test"}
    judgement_id         {1}
    category_id          {1}
    prefecture_id        {2}
    day_id               {1}
    status_id            {1}
    
    user    #Association with user

    after(:build) do |message|
      message.image.attach(io: File.open('public/images/a.png'), filename: 'a.png')
    end

  end
end

Loose commentary

-Items of "_id" such as judgment_id and category_id were introduced using active hash, so you can ignore them.

-The description "user" means an association with the user model. By describing this, you do not have to enter the user_id.

What I would like you to pay attention to this time is the description under user

ruby:ractories/items.rb(Model name.rb)


after(:build) do |message|
      message.image.attach(io: File.open('public/images/a.png'), filename: 'a.png')
    end

This description will refer to the actual photo.

Bad example

ruby:ractories/items.rb(Model name.rb)


FactoryBot.define do
  factory :item do
    name                 {"name"}
    price                {500}
    text                 {"test"}
    judgement_id         {1}
    category_id          {1}
    prefecture_id        {2}
    day_id               {1}
    status_id            {1}
    image                {"a.png "}    #This is a bad example
    
    user
  end
end

If you do this, only a character string can be registered, so an error will occur. By the way, the error content is as follows. If you want to check the error, write binding.pry and stop the process!

Terminal


[1] pry(#<RSpec::ExampleGroups::Item>)> @item.valid?
ActiveSupport::MessageVerifier::InvalidSignature: ActiveSupport::MessageVerifier::InvalidSignature

I hope it will be helpful for those who encounter the same error.

Recommended Posts

Image column test code when ActiveStorage is introduced
What is the model test code testing
Write code that is difficult to test
Efficient test code