This post summarizes the matchers I often use in RSpec.
"RSpec git URL" 「Relish」
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.
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.
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.
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.
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)
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