[RSpec] I wrote a test for uploading a profile image.

The Rails app that I'm currently developing has the ability to allow users to set a profile image. This time I wrote a test to see if the function is working properly, so please refer to it if you like!

【environment】

Test code

We are testing with system specifications.

spec/system/upload_avatar_spec.rb



require 'rails_helper'

RSpec.describe'UploadImage', type: :system do
  let(:user) { FactoryBot.create(:user) }
  let(:item) { FactoryBot.create(:item, user_id: user.id) }

  #Upload and save images
  def upload_user_avatar(user)
    valid_login(user)
    visit edit_user_path(user)
    attach_file 'user_avatar', "#{Rails.root}/spec/fixtures/images/test.jpg "
    click_on 'save'
  end

  it 'user successfully upload image user#show' do
    upload_user_avatar(user)
    expect(page).to have_selector("img[src$='test.jpg']")
  end

  it 'user successfully upload image on item#show' do
    upload_user_avatar(user)
    visit item_path(item)
    expect(page).to have_selector("img[src$='test.jpg']")
  end
end

The valid_login method is defined in `` `spec / support / login_support.rb```. The methods defined in this folder can be called during testing. I'm writing this code because my app only allows access to edit_user_path when I'm logged in. If you do not write it, it will fail. (I have to fail)

spec/support/login_support.rb



module LoginSupport
  def valid_login(user)
    visit root_path
    click_link 'Login'
    fill_in 'session[email]', with: user.email
    fill_in 'session[password]', with: user.password
    click_button 'Login'
  end
end

I will explain in order. First, generate test data for user and item with ** FactoryBot **. (It is necessary to define the test data in ``` spec / factries /` `` in advance. I will omit it here.)

The act of uploading an image is common to all blocks, so it is defined collectively as the upload_user_avatar method. In this method, you log in, go to the user's edit page, and use the attach_file method to upload and save the test image data. You need to prepare test images in `/ spec / fixtures / images /` in advance. Also, I specified the id of the input tag generated by f.file_field as the argument of the attach_file method and it worked! (See image. You can check the generated HTML from the developer tools.) スクリーンショット 2020-10-05 231134.png

We are doing two tests this time.

First, when the image is uploaded, it checks if the image data is in the redirect destination specified by the controller. I am using have_selector to check if the image data of test.jpg is included in the src attribute of the img tag.

The second one is almost the same. Since the same image is also used on the post detail page, I go to the post detail page and use have_selector to check if the image data of test.jpg is included in the src attribute of the img tag.

Thank you for reading to the end!

I output what I learned every day! !! If you have any impressions or suggestions, I would appreciate it if you could comment! !!

Recommended Posts

[RSpec] I wrote a test for uploading a profile image.
I wrote a primality test program in Java
I made a Docker image of SDAPS for Japanese
I wrote a test with Spring Boot + JUnit 5 now
I wrote a test code (Junit & mockit) for the code that calls the AWS API (Java)
I wrote a CRUD test with SpringBoot + MyBatis + DBUnit (Part 1)
How to test a private method with RSpec for yourself
I investigated Randoop, a JUnit test class generator for Java
Image upload using CarrierWave ~ Rspec test ~
I created a Docker image of a container for learning OpenAI Gym
Create a parent-child relationship form with form_object (I also wrote a test)
[Rspec] Flow from introducing Rspec to writing unit test code for a model
I want to write a unit test!
I made a plugin for IntelliJ IDEA
[RSpec] What I got stuck when I wrote the mailer (password reset process) test
I made a Diff tool for Java files
I made a primality test program in Java
I want to test Action Cable with RSpec test
I rewrote the Rails tutorial test with RSpec
[For beginners] Test devise user registration with RSpec
Introducing Rspec, a Ruby on Rails test framework
I wrote a prime factorization program in Java
Unit test code for a model using RSpec, which has a little peculiarity ~ User registration
[Out of 100 points] I thought about a coding test for programming school graduates [Ruby]