Skip to content

Instantly share code, notes, and snippets.

@danieljohnmorris
Forked from them0nk/rspec_rails_cheetsheet.rb
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danieljohnmorris/fc44aa0e61ca0adf9542 to your computer and use it in GitHub Desktop.
Save danieljohnmorris/fc44aa0e61ca0adf9542 to your computer and use it in GitHub Desktop.
#Model
@user.should have(1).error_on(:username) # Checks whether there is an error in username
@user.errors[:username].should include("can't be blank") # check for the error message
#Rendering
page.should render_template(:index)
#Redirecting
page.should redirect_to(movies_path)
#Capybara Matchers
page.should have_content("Hello world")
page.should have_no_content("Hello world")
page.should have_css("input#movie_title")
page.should have_css("input#movie_title", :value => "Twelve Angry Men")
page.should have_css("input", :count => 3) #True if there are 3 input tags in response
page.should have_css("input", :maximum => 3) # True if there or fewer or equal to 3 input tags
page.should have_css("input", :minimum => 3) # True if there are minimum of 3 input tags
page.should have_css("input", :between => 1..3) # True if there 1 to 3 input tags
page.should have_css("p a", :text => "hello") # True if there is a anchor tag with text hello
page.should have_css("p a", :text => /[hH]ello(.+)/i)
# True if there is a anchor tag with text matching regex
response.body.should have_xpath("//a")
response.body.should have_xpath("//a",:href => "google.com")
response.body.should have_xpath("//a[@href => 'google.com']")
response.body.should have_xpath("//a[contains(.,'some string')]")
response.body.should 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
page.should have_selector(:xpath, "//p/h1")
page.should have_selector(:css, "p a#movie_edit_path")
# For making capybara to take css as default selector
Capybara.default_selector = :css
page.should have_selector("input") #checks for the presence of the input tag
page.should have_selector("input", :value =>"Twelve Angry Men") # checks for input tag with value
page.should have_no_selector("input")
# For making capybara to take css as default selector
Capybara.default_selector = :xpath
page.should have_selector("//input") #checks for the presence of the input tag
page.should have_selector("//input", :value =>"Twelve Angry Men") # checks for input tag with value
# To access elements inside form
page.should have_field("FirstName") # checks for presence of a input field named FirstName in a form
page.should have_field("FirstName", :value => "Rambo")
page.should have_field("FirstName", :with => "Rambo")
page.should have_link("Foo")
page.should have_link("Foo", :href=>"googl.com")
page.should have_no_link("Foo", :href=>"google.com")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment