Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RSpec Introduction

Pat Allan
August 29, 2013

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
Tweet

More Decks by Pat Allan

Other Decks in Technology

Transcript

  1. describe ‘an rspec file’ do it ‘ends with _spec.rb’ do

    __FILE__.should match(/_spec\.rb$/) end end
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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?
  7. 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?
  8. 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?
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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