Share
Embed
RSpec Introduction
A very rough, quick guide to RSpec. Probably not much use if you didn't see this talk at Melbourne Ruby in August.
Pat Allan
August 29, 2013
Transcript
21 slides
-
describe ‘an rspec file’ do it ‘ends with _spec.rb’ do
__FILE__.should match(/_spec\.rb$/) end end -
describe ‘Melbourne Ruby’ do context ‘From January to November’ do
it ‘is on the last Thursday’ do # ... end end context ‘In December’ do it ‘happens earlier’ do # ... end it ‘has no talks’ do # ... end end end -
describe ‘Melbourne Ruby’ do context ‘From January to November’ do
it ‘is on the last Thursday’ do # ... end end context ‘In December’ do it ‘happens earlier’ do # ... end it ‘has no talks’ do # ... end end end -
describe ‘User signs up’ do before :each do ActionMailer::Base.deliveries.clear end
it ‘receives an email’ do # ... end end -
require ‘spec_helper’ it ‘signs user in automatically’ do visit root_path
click_link ‘Sign up’ fill_in ‘Email’, with: ‘[email protected]' fill_in ‘Password’, with: ‘mysecret’ click_button ‘Sign up’ expect(page).to have_content(‘Sign out’) # or page.should have_content(‘Sign out’) end -
require ‘spec_helper’ it ‘signs user in automatically’ do visit root_path
click_link ‘Sign up’ fill_in ‘Email’, with: ‘[email protected]' fill_in ‘Password’, with: ‘mysecret’ click_button ‘Sign up’ expect(page).to have_content(‘Sign out’) # or page.should have_content(‘Sign out’) end load everything -
require ‘spec_helper’ it ‘signs user in automatically’ do visit root_path
click_link ‘Sign up’ fill_in ‘Email’, with: ‘[email protected]' fill_in ‘Password’, with: ‘mysecret’ click_button ‘Sign up’ expect(page).to have_content(‘Sign out’) # or page.should have_content(‘Sign out’) end where do we need to be? -
require ‘spec_helper’ it ‘signs user in automatically’ do visit root_path
click_link ‘Sign up’ fill_in ‘Email’, with: ‘[email protected]' fill_in ‘Password’, with: ‘mysecret’ click_button ‘Sign up’ expect(page).to have_content(‘Sign out’) # or page.should have_content(‘Sign out’) end what do we need to do? -
require ‘spec_helper’ it ‘signs user in automatically’ do visit root_path
click_link ‘Sign up’ fill_in ‘Email’, with: ‘[email protected]' fill_in ‘Password’, with: ‘mysecret’ click_button ‘Sign up’ expect(page).to have_content(‘Sign out’) # or page.should have_content(‘Sign out’) end what should we see? -
require 'active_resource/exceptions' require './lib/exchequer' describe Exchequer::Subscription do let(:card) { double(‘Card’,
:first_name => 'John', :last_name => 'Smith', :to_hash => double) } let(:subscription) { double(‘Subscription’, :update_attributes => true) } describe '#update' do before :each do stub_const 'Chargify::Subscription', double(:find => subscription) stub_const 'Exchequer::ErrorMapper', double(:map => true) end -
require 'active_resource/exceptions' require './lib/exchequer' describe Exchequer::Subscription do let(:card) { double(‘Card’,
:first_name => 'John', :last_name => 'Smith', :to_hash => double) } let(:subscription) { double(‘Subscription’, :update_attributes => true) } describe '.update' do before :each do stub_const 'Chargify::Subscription', double(:find => subscription) stub_const 'Exchequer::ErrorMapper', double(:map => true) end only what’s necessary -
require 'active_resource/exceptions' require './lib/exchequer' describe Exchequer::Subscription do let(:card) { double(‘Card’,
:first_name => 'John', :last_name => 'Smith', :to_hash => double) } let(:subscription) { double(‘Subscription’, :update_attributes => true) } describe '#update' do before :each do stub_const 'Chargify::Subscription', double(:find => subscription) stub_const 'Exchequer::ErrorMapper', double(:map => true) end test doubles -
require 'active_resource/exceptions' require './lib/exchequer' describe Exchequer::Subscription do let(:card) { double(‘Card’,
:first_name => 'John', :last_name => 'Smith', :to_hash => double) } let(:subscription) { double(‘Subscription’, :update_attributes => true) } describe '#update' do before :each do stub_const 'Chargify::Subscription', double(:find => subscription) stub_const 'Exchequer::ErrorMapper', double(:map => true) end more test doubles -
it "finds the subscription with given id" do Chargify::Subscription. should_receive(:find).with(4853).
and_return(subscription) Exchequer::Subscription.update( 4853, :card => card ) end it "updates the subscription" do subscription.should_receive(:update_attributes). with( :credit_card_attributes => card.to_hash ).and_return(true) Exchequer::Subscription.update( 4853, :card => card ) end -
it "finds the subscription with given id" do Chargify::Subscription. should_receive(:find).with(4853).
and_return(subscription) Exchequer::Subscription.update( 4853, :card => card ) end it "updates the subscription" do subscription.should_receive(:update_attributes). with( :credit_card_attributes => card.to_hash ).and_return(true) Exchequer::Subscription.update( 4853, :card => card ) end method expectation -
context 'subscription cannot be saved' do before :each do subscription.stub
:update_attributes => false end it "maps the subscription errors to the card" do Exchequer::ErrorMapper.should_receive(:map). with(subscription, :to => card). and_return(true) Exchequer::Subscription.update 4853, :card => card end end -
context 'subscription cannot be saved' do before :each do subscription.stub
:update_attributes => false end it "maps the subscription errors to the card" do Exchequer::ErrorMapper.should_receive(:map). with(subscription, :to => card). and_return(true) Exchequer::Subscription.update 4853, :card => card end end stubbing a method