[Rails] Integration test using Capybara (from introduction to simple test execution)

Introduction

An integration test is a test that confirms that the app works as intended. You can do it yourself on the browser, but writing the test code has a big advantage, so I think it's better to write it if possible.

merit ** ・ Once you write the code, you can easily test it ** (If you don't have a test code, you have to fill out the form and move the link every time.) ** ・ You can organize your thoughts ** By writing it down, you can objectively view your thoughts and make it easier to notice omissions. ** ・ Can be recorded as a record ** You can look back on how much you expect the app to work In addition, a third party can easily check the test contents.

This time, I will check if the posting operation works like the following GIF file. e02d69df1a47ecac2b896cfeb09626d2.gif

Introducing Capybara

First, we will introduce a gem called Capybara.

Gemfile


group :test, :development do
  #(abridgement)
  gem 'capybara'
end

Terminal


$ bundle install

spec/rails_helper.rb


#Added the following description
require 'capybara/rspec'

Next, prepare a working file. Let's create the spec / features directory. Create a test file in it. This time it is post_spec.rb.

Test code

Describe it in post_spec.rb.

spec/features/post_spec.rb


require 'rails_helper'

feature 'post', type: :feature do
  scenario 'Being able to post' do
    #There is a post button
    visit root_path
    expect(page).to have_content('Post')

    #Post processing works
    expect {
      click_link('Post')
      expect(current_path).to eq new_post_path
      fill_in 'post_content', with: 'Hello'
      fill_in 'post_tags_attributes_0_content', with: 'tag'
      find('input[type="submit"]').click
    }.to change(Post, :count).by(1)

    #Redirected to the top page
    expect(current_path).to eq root_path

    #The posted content is displayed on the top page
    expect(page).to have_content('Hello')
  end
end

Describe a series of operations between senario do ~ end.

This time, we confirm that the post link is displayed, confirm the post process, confirm the redirect, and confirm the display of the posted content in order. I will not explain the details of each process such as move to the specified path with visit, fill in the form with fill_in, post with find or click, but it will come out when you check it, so let's find if there is a process you want to do ..

This cheat sheet has a list of things that can be used with capybara, so you may want to refer to it.

Test run

It is good to execute the test by specifying the file as follows.

Terminal


$ bundle exec rspec spec/features/post_spec.rb

Specify the file you want to execute under spec. This time I wrote the test code in post_spec.rb in the features directory, so it looks like the above.

Let's run it.

12172459f80a1f0c542410796c6829fe.png If it is displayed as above, it has been cleared successfully.

Recommended Posts

[Rails] Integration test using Capybara (from introduction to simple test execution)
Introduction to RSpec 1. Test, RSpec
[Rails] From test preparation to model unit testing [RSpec]
Introduction to Micronaut 2 ~ Unit test ~
[Rails] Test code using Rspec
Error when using rails capybara
[Integration test code] How to select an element from date_select
Introduction to RSpec-Everyday Rails Summary-
Ruby on Rails --From environment construction to simple application development on WSL2
I tried to build a simple application using Dockder + Rails Scaffold
From introduction to use of ActiveHash
Introduction to Ruby (from other languages)
From introduction to usage of byebug
[Introduction to Rails] form_with (local: true)
[Introduction to Rails] How to use render
[Rails] Reflection to db using seeds.rb
From pulling docker-image of rails to launching
Introduction memo of automatic test using Jenkins
[Rails] How to upload images using Carrierwave
Connect from Java to MySQL using Eclipse
[Rails] Assigning variables from controller to JavaScript
[Java] Flow from source code to execution
Introduction to monitoring from Java Touching Prometheus
How to implement image posting using rails
[Rails] How to handle data using enum
How to resolve errors that occur in the "Ruby on Rails" integration test
[Rails] I want to test with RSpec. We support your step [Introduction procedure]