Forked from ericbrooke/Rails testing with RSpec, Capybara and RSpec-Candy
Created
March 25, 2014 01:14
-
-
Save expede/9753517 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#test for editing | |
#setup on the command line | |
gem install rspec | |
rspec --init | |
#add to gemfile: | |
group :test, :development do | |
gem 'rspec-rails' | |
end | |
# on the command line | |
bundle install | |
rails generate rspec:install | |
#create .rspec and save in the file the following: | |
-- color | |
-- format documentaion | |
# feature Specs: | |
feature Page_Name do | |
scenario '' do | |
it '' do | |
end | |
end | |
end | |
# model specs: | |
# unit Tests: | |
describe Model_Name do | |
describe 'Method_name' do | |
it '' do | |
end | |
end | |
end | |
# before the method_name | |
# '#' class methods | |
# '.' instance methods | |
# matchers: | |
# for text content - | |
expect(page).to have_content('Privacy Policy') | |
# for CSS - | |
expect(page).to have_css('.support-form') | |
# Note . = class, # = id | |
# for selector e.g. <footer> or <a> - | |
expect(page).to have_selector('footer') | |
# match a regular expression - | |
animal.type should_match(/hamster/) | |
# counting number - | |
human.hands.count should == 2 | |
human.should have(2).hands | |
human.should have_at_least(2).hands | |
human.should have_at_most(2).hands | |
human.should be_within(1).of(10) | |
# monitoring change - | |
expect { human.save }.to change { Human.count }.by(1) | |
expect { human.save }.to change { Human.count }.by(1).from(1) | |
expect { human.save }.to change { Human.count }.by(1).to(2) | |
# raise_exception - | |
expect { human.save }.to raise_error (ActiveRecord::RecordInvalid) | |
# for links - | |
expect(page).to have_link('Privacy Policy', href: "Privacy_Policy.pdf") | |
# for testing type - | |
expect(user).to be_a(Hash) | |
# testing collections - | |
Content.find_contents_for_user(user.id).should include content1, content2 #does not care about order | |
Content.find_contents_for_user(user.id).should eq content1 | |
Content.find_contents_for_user(user.id).should be content1 | |
expect(User.most_liked(1)).to start_with(user) | |
# method testing - | |
human.should respond_to(alive?) | |
# other matchers - | |
human.should exist | |
human.should satisfy { |human| human.hungry? } | |
creature.should be_kind_of(Human) | |
human.name.should be_an_instance_of(String) | |
# testing callbacks - | |
# Use 'RSpec-candy' Gem | |
describe Content, '#after_create' do | |
it_should_run_callbacks :set_short_url, :set_new_content, :send_notification | |
end | |
# testing with one subject allows you define the subject and then use it {} statements | |
subject { content } | |
it { should be_valid } or it { should_not be_valid } | |
# implicit subject/receiver | |
# if you use a class name in your describe block i.e. 'describe Human do' you have five further options: | |
subject.should respond_to(:name) | |
OR | |
should respond_to(:name) | |
describe Human do | |
it 'responds to name do | |
should respond_to(:name) | |
end | |
end | |
describe Human do | |
it 'responds to name' { should respond_to(:name) } | |
end | |
describe Human do | |
it { should respond_to(:name) } | |
end | |
describe Human do | |
its(:name) { should == 'Eric' } | |
end | |
# more then one subject use let and define subject in block | |
context 'poor human' do | |
let(:human) { Human.new(income: poor, clothes: [knackered]) } | |
let(:knackered) { Clothes.new(name: 'knackered') } | |
subject { human } | |
its (:clothes) { should include(knackered) } | |
it 'can wear its knackered clothes' do | |
human.wear(knackered).should == true | |
end | |
end | |
#newer RSpec allow you to combine the the subject and let line | |
subject(:human) { Human.new(income: poor, clothes: [knackered]) } | |
#Lazy evaluation and Let! - let does not create the object until the rest calls it, Let! creates it and puts it in the database | |
it "creates a human" { Human.count == 1 } # this would fail if the human was created with let but not Let! | |
# debugging tests: | |
# use 'byebug' Gem, add to Gemfile and bundle install | |
# insert the word byebug at the top of the it '' block | |
# use puts to output variables | |
# use sleep(30) to check the page | |
# use 'rails console' to understand the data that goes in and out | |
# good Resources: | |
e-book - http://everydayrails.com | |
webpage - http://betterspecs.org | |
Codeschool - $29 per month - has four courses - https://www.codeschool.com/courses/testing-with-rspec | |
Rails4 in Action - http://www.manning.com/bigg2/ | |
Railscast 158 - Factories - http://railscasts.com/episodes/158-factories-not-fixtures-revised | |
Railscast 275 - How I test - http://railscasts.com/episodes/275-how-i-test | |
Railscast 257 - Request Specs and Capybara - http://railscasts.com/episodes/275-how-i-test | |
webpage - http://railsadventures.wordpress.com/2013/09/25/11-steps-to-make-your-rspec-specs-awesome/ | |
webpage - http://gaslight.co/blog/6-ways-to-remove-pain-from-feature-testing-in-ruby-on-rails | |
# RSpec Maintainers: | |
http://www.andylindeman.com | |
http://myronmars.to/n/dev-blog | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment