[RAILS] Matcher often used in RSpec

This post summarizes the matchers I often use in RSpec.

"RSpec git URL" 「Relish

  1. include include is a matcher that verifies that the expect argument contains the include argument.
describe "Sports" do
  it "Baseball is included in sports" do
     expect(["baseball", "Football", "rugby", "tennis"]).to include("baseball")
  end
end

If you run this test code, the test succeeds'' because the array contains baseball'' as expected. 2. eq eq is a matcher that ensures that the expect argument and the eq argument are equal.

describe "addition" do
  it "1 +The calculation result of 1 is equal to 2." do
     expect(1 + 1).to eq(2)      # 1 + 1 = 2 =>correct
  end
end

If you run this test code, the test succeeds'' because 1 + 1is equal to 2`` as expected.

  1. be_valid be_valid is a matcher that ensures that the return value when performing validation is true.
#Up to 5 characters for user nickname in validation
it "Nickname can be registered if it is 5 characters or less" do
  user.nickname = "AIUEO"      #Overwrite nickname
  expect(user).to be_valid       # be_Determine if user can be registered with valid
end

If you run this test code, the test succeeds'' because nicknameis 5 characters or less`` as expected.

  1. have_content expect(page).to have_content('X') It is a matcher that determines whether there is a character string X in the visited page. page stores as much information as you can see on the page you visited. For example, if the page has a button labeled "Login", the string information "Login" is stored in page. However, the character string that can be seen only by hovering the cursor is not included in page.
#Make sure there is a "login button" on the login page
expect(page).to have_content('Login')

There is also a have_no_content matcher that is the opposite of have_content and makes sure that the string doesn't exist.

  1. have_link expect (" element"). to have_link'button string', href: "link path" A matcher that confirms that there is a link that fits in the element. Also, have_link is used for a element.
#There is a path to log in on the login page
expect(page).to have_link 'Login', href: new_user_session_path  #This code uses page instead of element.

There is also a have_no_link matcher that is the opposite of have_link and confirms that there is no applicable link. expect (" element"). To have_no_link'button string', href: "link destination path" to make sure there are no applicable links in the element.

  1. change expect {"some action"} .to change {" something that changes in number"}. By (X) It is a matcher to check how many (X) numbers change.
#Click the "Post" button on the page_Click with on method
expect{ click_on "Post" }.to change { Tweet.count }.by(1)   #Tweet model record count goes up by one
#Check how many changes for the variable a
expect { a += 1 }.to change { a }.by(1)            #Validate increase / decrease of value with relative value
# => a = 2     
expect { a += 3 }.to change { a }.from(2).to(5)    #Verify what the state has changed from
# => a = 5 (a changes from 2 to 5)

Summary

As for the RSpec matchers introduced this time, I learned that there are still many matchers while studying only a small part. I would like to continue to post new findings and lessons learned. If you have any mistakes, please let us know in the comments.

Recommended Posts

Matcher often used in RSpec
Gem often used in Rails
Syntax examples often used in Java
About methods often used in devise
Test API often used in AssertJ
Ruby methods often used in Rails
Personal summary of the guys often used in JUnit 4
About method matchers used in model unit test code (RSpec)
Mock and stub in RSpec
Mechanism and characteristics of Collection implementation class often used in Java
Formulas often used when making animations
Frequently used methods in Active Record
Expression used in the fizz_buzz problem
Library summary that seems to be often used in recent Android development (2019/11)