Slide 1

Slide 1 text

r s p e c Find out what it means to me.

Slide 2

Slide 2 text

rspec.info

Slide 3

Slide 3 text

relishapp.com/rspec

Slide 4

Slide 4 text

describe

Slide 5

Slide 5 text

RSpec.describe Array do describe '.new' do ... end describe '#count' do ... end end

Slide 6

Slide 6 text

context

Slide 7

Slide 7 text

describe '#count' do context 'given an object' do ... end context 'given a block' do ... end end

Slide 8

Slide 8 text

it

Slide 9

Slide 9 text

describe '#count' do context 'given an object' do it 'counts the occurrences' do ... end end end

Slide 10

Slide 10 text

expect

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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)

Slide 13

Slide 13 text

let

Slide 14

Slide 14 text

expect( ['a', 'b', 'c', 'c'].count('c') ).to eql 2

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

before/after

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

running tests

Slide 20

Slide 20 text

> rspec array_spec.rb .. Finished in 0.00092 seconds (files took 0.14958 seconds to load) 2 examples, 0 failures

Slide 21

Slide 21 text

> 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 ' 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

Slide 22

Slide 22 text

> 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

Slide 23

Slide 23 text

Nyan Cat RSpec Formatter https://github.com/mattsears/nyan-cat-formatter

Slide 24

Slide 24 text

feature specs

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

mocks/stubs/spies

Slide 27

Slide 27 text

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)

Slide 28

Slide 28 text

rspec-rails • generators • custom matchers • tests for: • models • views • controller • helpers • requests • routes

Slide 29

Slide 29 text

fin