Forked from nerdinand/rspec_rails_cheetsheet.rb
Last active
October 23, 2022 21:00
-
-
Save redrick/01a0ca575a99f264f1d8ab6a4c93cff0 to your computer and use it in GitHub Desktop.
New expect syntax + new hash syntax and couple corrections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Model | |
expect(@user).to have(1).error_on(:username) # Checks whether there is an error in username | |
expect(@user.errors[:username]).to include("can't be blank") # check for the error message | |
#Rendering | |
expect(response).to render_template(:index) | |
#Redirecting | |
expect(response).to redirect_to(movies_path) | |
#Capybara Matchers | |
expect(response.body).to have_content("Hello world") | |
expect(response.body).to have_no_content("Hello world") | |
expect(response.body).to have_css("input#movie_title") | |
expect(response.body).to have_css("input#movie_title", text: "Twelve Angry Men") | |
expect(response.body).to have_css("input", count: 3) #True if there are 3 input tags in response | |
expect(response.body).to have_css("input", maximum: 3) # True if there or fewer or equal to 3 input tags | |
expect(response.body).to have_css("input", minimum: 3) # True if there are minimum of 3 input tags | |
expect(response.body).to have_css("input", between: 1..3) # True if there 1 to 3 input tags | |
expect(response.body).to have_css("p a", text: "hello") # True if there is a anchor tag with text hello | |
expect(response.body).to have_css("p a", text: /[hH]ello(.+)/i) | |
# True if there is a anchor tag with text matching regex | |
expect(response.body).to have_xpath("//a") | |
expect(response.body).to have_xpath("//a", href: "google.com") | |
expect(response.body).to have_xpath("//a[@href => 'google.com']") | |
expect(response.body).to have_xpath("//a[contains(.,'some string')]") | |
expect(response.body).to have_xpath("//p//a", text: /re[dab]i/i, count: 1) | |
# can take both xpath and css as input and can take arguments similar to both have_css and have_xpath | |
expect(response.body).to have_selector(:xpath, "//p/h1") | |
expect(response.body).to have_selector(:css, "p a#movie_edit_path") | |
# For making capybara take css as default selector | |
Capybara.default_selector = :css | |
expect(response.body).to have_selector("input") #checks for the presence of the input tag | |
expect(response.body).to have_selector("input", text: "Twelve Angry Men") # checks for input tag with value | |
expect(response.body).to have_no_selector("input") | |
# For making capybara take xpath as default selector | |
Capybara.default_selector = :xpath | |
expect(response.body).to have_selector("//input") #checks for the presence of the input tag | |
expect(response.body).to have_selector("//input", text: "Twelve Angry Men") # checks for input tag with value | |
# To access elements inside form | |
expect(response.body).to have_field("FirstName") # checks for presence of a input field named FirstName in a form | |
expect(response.body).to have_field("FirstName", text: "Rambo") | |
expect(response.body).to have_field("FirstName", with: "Rambo") | |
expect(response.body).to have_link("Foo") | |
expect(response.body).to have_link("Foo", href: "googl.com") | |
expect(response.body).to have_no_link("Foo", href: "google.com") |
@johnflux I am not sure I am following you on that one... you got the response.body itself, print that out and you should be able to find wha tyou look for there... if it's longer grep it 🤷♂️ or
binding.pry
in that place and go over manualy printing out what you want from console?
Yeah I tried that. The issue that I had, is that have_link actually encodes the url you give it.
In the printed out response.body the href included a %22 and I was coping and pasting that exactly into have_link. And it was saying no match.
But actually you have to decode the url and pass in a literal speech mark.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@johnflux I am not sure I am following you on that one... you got the response.body itself, print that out and you should be able to find wha tyou look for there... if it's longer grep it 🤷♂️ or
binding.pry
in that place and go over manualy printing out what you want from console?