Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

rspec

 rspec

A quick introduction given on RSpec to the Dallas Ruby Brigade.

Aaron Lasseigne

June 02, 2015
Tweet

More Decks by Aaron Lasseigne

Other Decks in Programming

Transcript

  1. describe '#count' do context 'given an object' do ... end

    context 'given a block' do ... end end
  2. it

  3. describe '#count' do context 'given an obj' do it 'counts

    the occurrences' do expect( ['a', 'b', 'c', 'c'].count('c') ).to eql 2 end end end
  4. expect(3).to be > 2 expect(2).to_not be > 3 expect(nil).to be_nil

    # checks .nil? expect(42).to be_truthy # not nil or false expect(a: 1).to have_key(:a) expect(5).to be_between(1, 5) expect(5).to_not be_between(1, 5).exclusive expect(5.3).to be_within(0.5).of(5)
  5. let

  6. describe '#count' do let(:count) { 2 } let(:obj) { 'c'

    } let(:list) do ['a', 'b'] + Array.new(count, obj) end context 'given an obj' do it 'counts the occurrences' do expect(list.count(obj)).to eql count end end end
  7. describe '#count' do let(:count) { 2 } let(:obj) { 'c'

    } let(:list) { ['a', 'b'] + Array.new(count, obj) } context 'given an obj' do ... end context 'given a block' do it 'counts the occurrences' do expect( list.count { |o| o == obj } ).to eql count end end end
  8. before(:context) do # do once before all tests in scope

    end before(:example) do # default # do before each test in scope end after(:context) do # do once after all tests in scope end after(:example) do # default # do after each test in scope end
  9. > rspec array_spec.rb .. Finished in 0.00092 seconds (files took

    0.14958 seconds to load) 2 examples, 0 failures
  10. > rspec array_spec.rb F. Failures: 1) Array#count given an obj

    counts the occurrences Failure/Error: expect(list.count(obj)).to eql count + 1 expected: 3 got: 2 (compared using eql?) # ./array_spec.rb:12:in `block (3 levels) in <top (required)>' Finished in 0.00103 seconds (files took 0.13107 seconds to load) 2 examples, 1 failure Failed examples: rspec ./array_spec.rb:11 # Array#count given an obj counts the occurrences
  11. > rspec array_spec.rb --format documentation Array #count given an obj

    counts the occurrences given a block counts the occurrences Finished in 0.00107 seconds (files took 0.12905 seconds to load) 2 examples, 0 failures
  12. require 'rails_helper' RSpec.feature 'Widget management', type: :feature do scenario 'User

    creates a new widget' do visit '/widgets/new' fill_in 'Name', with: 'My Widget' click_button 'Create Widget' expect(page).to have_text('Widget was successfully created.') end end
  13. book = double('book', title: 'The RSpec Book') book = Book.new

    allow(book).to receive(:title).and_return('The RSpec Book') invitation = spy('invitation') user.accept_invitation(invitation) expect(invitation).to have_received(:accept)
  14. rspec-rails • generators • custom matchers • tests for: •

    models • views • controller • helpers • requests • routes