Skip to content

Instantly share code, notes, and snippets.

@iannono
Forked from them0nk/rspec_rails_cheetsheet.rb
Last active December 22, 2015 02:29
Show Gist options
  • Save iannono/6403867 to your computer and use it in GitHub Desktop.
Save iannono/6403867 to your computer and use it in GitHub Desktop.
#Model
@user.should have(2).children # check assosiation
@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
expect { @user.save }.to change { User.count }.by(1) # or from(1).to(2)
expect { @user.save! }.to raise_error(ActivieRecord::RecordInvalid)
#More matchers
@person.should respond_to(:child?)
@person.children.should be_within(1).of(3)
@person.children.should exist
@person.should satisfy { |user| user.child? }
@old_person.should be_kind_of(Person)
@xiongbo.should be_an_instance_of(Person)
#Rendering
response.should render_template(:index)
#Redirecting
response.should redirect_to(movies_path)
#DRY
describe Person do
it 'responds to name' do
subject.should respond_to(:name) #subject = Person.new
#=> should respond_to(:name)
end
#= it { should respond_to(:name) }
its(:name) { should == "xiongbo" }
its('children.size') { should == 2 }
end
#stub
target.should_receive(:function).once
.twice
.exactly(3).times
.at_least(2).times
.at_most(3).times
.any_number_of_times
target.should_receive(:function).with(no_args())
.with(any_args())
.with("B", anything())
.with(3, kind_of(Numeric))
.with(/zombie ash/)
#Capybara Matchers
response.body.should have_content("Hello world")
response.body.should have_no_content("Hello world")
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")
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")
# 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
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")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment