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

RSpec Introduction

29f82ebe1801087f04de6aaae92e19ea?s=47 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.

29f82ebe1801087f04de6aaae92e19ea?s=128

Pat Allan

August 29, 2013
Tweet

More Decks by Pat Allan

Other Decks in Technology

Transcript

  1. RSpec Pat Allan / @pat http://freelancing-gods.com

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

    __FILE__.should match(/_spec\.rb$/) 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. 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
  5. describe ‘User signs up’ do before :each do ActionMailer::Base.deliveries.clear end

    it ‘receives an email’ do # ... end end
  6. Acceptance Specs

  7. require ‘spec_helper’ it ‘signs user in automatically’ do visit root_path

    click_link ‘Sign up’ fill_in ‘Email’, with: ‘pat@rspec.info' fill_in ‘Password’, with: ‘mysecret’ click_button ‘Sign up’ expect(page).to have_content(‘Sign out’) # or page.should have_content(‘Sign out’) end
  8. require ‘spec_helper’ it ‘signs user in automatically’ do visit root_path

    click_link ‘Sign up’ fill_in ‘Email’, with: ‘pat@rspec.info' 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
  9. require ‘spec_helper’ it ‘signs user in automatically’ do visit root_path

    click_link ‘Sign up’ fill_in ‘Email’, with: ‘pat@rspec.info' 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?
  10. require ‘spec_helper’ it ‘signs user in automatically’ do visit root_path

    click_link ‘Sign up’ fill_in ‘Email’, with: ‘pat@rspec.info' 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?
  11. require ‘spec_helper’ it ‘signs user in automatically’ do visit root_path

    click_link ‘Sign up’ fill_in ‘Email’, with: ‘pat@rspec.info' 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?
  12. Unit Specs

  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. Thanks! Read the RSpec Book!