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.
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
.
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.
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.
If it is displayed as above, it has been cleared successfully.Recommended Posts