Skip to content

Instantly share code, notes, and snippets.

@sfkaos
Forked from them0nk/rspec_rails_cheetsheet.rb
Created February 24, 2014 21:48

Revisions

  1. @them0nk them0nk revised this gist Mar 24, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rspec_rails_cheetsheet.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    #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
    response.should render_template(:index)
  2. @them0nk them0nk revised this gist Mar 24, 2012. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions rspec_rails_cheetsheet.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,8 @@
    #Model

    @user.should have(1).error_on(:username) # Checks whether there is an error in username


    #Rendering
    response.should render_template(:index)

  3. @them0nk them0nk revised this gist Mar 23, 2012. 1 changed file with 13 additions and 2 deletions.
    15 changes: 13 additions & 2 deletions rspec_rails_cheetsheet.rb
    Original file line number Diff line number Diff line change
    @@ -21,15 +21,26 @@

    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
    response.body.should have_selector(:xpath, "//p/h1")
    response.body.should have_selector(:css, "p a#movie_edit_path")

    # For making capybara to take css as default selector
    Capybara.default_selector = :css
    response.body.should have_selector("input") #checks for the presence of the input tag
    response.body.should have_selector("input", :value =>"Twelve Angry Men") # checks for input tag with value
    response.body.should have_no_selector("input")

    response.body.should have_selector(:xpath, "//p/h1")
    response.body.should have_selector(:css, "p a#movie_edit_path")
    # For making capybara to take css as default selector
    Capybara.default_selector = :xpath
    response.body.should have_selector("//input") #checks for the presence of the input tag
    response.body.should have_selector("//input", :value =>"Twelve Angry Men") # checks for input tag with value


    # To access elements inside form
    response.body.should have_field("FirstName") # checks for presence of a input field named FirstName in a form
  4. @them0nk them0nk revised this gist Mar 23, 2012. 1 changed file with 10 additions and 5 deletions.
    15 changes: 10 additions & 5 deletions rspec_rails_cheetsheet.rb
    Original file line number Diff line number Diff line change
    @@ -4,15 +4,11 @@
    #Redirecting
    response.should redirect_to(movies_path)

    #Matchers
    #Capybara Matchers

    response.body.should have_content("Hello world")
    response.body.should have_no_content("Hello world")

    response.body.should have_selector("input") #checks for the presence of the input tag
    response.body.should have_selector("input", :value =>"Twelve Angry Men") # checks for input tag with value
    response.body.should have_no_selector("input")

    response.body.should have_css("input#movie_title")
    response.body.should have_css("input#movie_title", :value => "Twelve Angry Men")
    response.body.should have_css("input", :count => 3) #True if there are 3 input tags in response
    @@ -26,6 +22,15 @@
    response.body.should have_xpath("//a")
    response.body.should have_xpath("//a",:href => "google.com")

    # can take both xpath and css as input and can take arguments similar to both have_css and have_xpath

    response.body.should have_selector("input") #checks for the presence of the input tag
    response.body.should have_selector("input", :value =>"Twelve Angry Men") # checks for input tag with value
    response.body.should have_no_selector("input")

    response.body.should have_selector(:xpath, "//p/h1")
    response.body.should have_selector(:css, "p a#movie_edit_path")

    # To access elements inside form
    response.body.should have_field("FirstName") # checks for presence of a input field named FirstName in a form
    response.body.should have_field("FirstName", :value => "Rambo")
  5. @them0nk them0nk revised this gist Mar 23, 2012. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions rspec_rails_cheetsheet.rb
    Original file line number Diff line number Diff line change
    @@ -15,8 +15,24 @@

    response.body.should have_css("input#movie_title")
    response.body.should have_css("input#movie_title", :value => "Twelve Angry Men")
    response.body.should have_css("input", :count => 3) #True if there are 3 input tags in response
    response.body.should have_css("input", :maximum => 3) # True if there or fewer or equal to 3 input tags
    response.body.should have_css("input", :minimum => 3) # True if there are minimum of 3 input tags
    response.body.should have_css("input", :between => 1..3) # True if there 1 to 3 input tags
    response.body.should have_css("p a", :text => "hello") # True if there is a anchor tag with text hello
    response.body.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")

    # To access elements inside form
    response.body.should have_field("FirstName") # checks for presence of a input field named FirstName in a form
    response.body.should have_field("FirstName", :value => "Rambo")
    response.body.should have_field("FirstName", :with => "Rambo")

    response.body.should have_link("Foo")
    response.body.should have_link("Foo", :href=>"googl.com")
    response.body.should have_no_link("Foo", :href=>"google.com")


  6. @them0nk them0nk revised this gist Mar 23, 2012. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion rspec_rails_cheetsheet.rb
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,9 @@
    response.body.should have_no_selector("input")

    response.body.should have_css("input#movie_title")
    response.body.should have_css("input#movie_title", :value => "Twelve Angry Men")


    response.body.should have_xpath("//a")
    response.body.should have_xpath("//a",:href => "google.com")


  7. @them0nk them0nk revised this gist Mar 23, 2012. 1 changed file with 13 additions and 3 deletions.
    16 changes: 13 additions & 3 deletions rspec_rails_cheetsheet.rb
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,20 @@
    #Rendering & Redirecting

    #Rendering
    response.should render_template(:index)

    #Redirecting
    response.should redirect_to(movies_path)

    #Matching
    #Matchers

    response.body.should have_content("Hello world")
    response.body.should have_no_content("Hello world")

    response.body.should have_selector("input") #checks for the presence of the input tag
    response.body.should have_selector("input", :value =>"Twelve Angry Men") # checks for input tag with value
    response.body.should have_no_selector("input")

    response.body.should have_css("input#movie_title")




  8. @them0nk them0nk revised this gist Mar 23, 2012. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion rspec_rails_cheetsheet.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    #Rendering & Redirecting

    response.should render_template(:index)
  9. @them0nk them0nk created this gist Mar 23, 2012.
    11 changes: 11 additions & 0 deletions rspec_rails_cheetsheet.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@

    #Rendering & Redirecting

    response.should render_template(:index)
    response.should redirect_to(movies_path)

    #Matching

    response.body.should have_content("Hello world")