Slide 1

Slide 1 text

www.codingzeal.com @adamcuppy Adam Cuppy - CodingZeal.com - Custom Web & Mobile Apps - Consultants for hire [email protected]

Slide 2

Slide 2 text

github.com/acuppy twitter.com/adamcuppy - @AdamCuppy

Slide 3

Slide 3 text

speakerdeck.com/ acuppy

Slide 4

Slide 4 text

http://rspec.info/ * Started in 2005 as an experiment by Steven Baker * Behavior Driven Development testing framework for Ruby * Declarative DSL (describe, context, it/specify, etc…) * Test framework for many ruby libraries/gems

Slide 5

Slide 5 text

Problem * Our test suite becomes a second class citizen * DSL is thick and the learning curve can be tough * “BUT WE MUST HAVE TESTS!!” So, we deal. * Tests are hard to understand, expand, refactor and confusing

Slide 6

Slide 6 text

Taming Chaotic Specs RSpec Design Patterns

Slide 7

Slide 7 text

Not what to test, But…

Slide 8

Slide 8 text

Patterns to follow when structuring the test(s)

Slide 9

Slide 9 text

1. Communicate expectations 2. Encourage consistency 3. Reduce the mental load Design Patterns… * Expectations: Ruby is a communicative language * Consistency: 2+ people = communication * Mental load: you don’t to spend cycles digesting what’s being communicated

Slide 10

Slide 10 text

Minimum Valid Object

Slide 11

Slide 11 text

1. Start with a “valid” object 2. Make one specific change 3. Assert the “valid” object is now “invalid” M.V.O. Workflow…

Slide 12

Slide 12 text

# app/models/user.rb class User < ActiveRecord::Base has_secure_password validates :firstname, presence: true, length: 4..20 validates :middlename, length: 4..20, allow_blank: true validates :lastname, presence: true, length: 4..20 validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i } validates :password, presence: true, confirmation: true, length: { minimum: 8 } # ... end

Slide 13

Slide 13 text

# spec/models/user_spec.rb describe User do it "should be invalid" do user = User.new(firstname: "really really ... super long firstname") expect(user.valid?).to_not eq true user = User.new(firstname: "sht") expect(user.valid?).to_not eq true end end

Slide 14

Slide 14 text

# spec/models/user_spec.rb describe User do it "should be invalid" do user = User.new(firstname: "really really ... super long firstname") expect(user.valid?).to_not eq true user = User.new(firstname: "sht") expect(user.valid?).to_not eq true end end

Slide 15

Slide 15 text

# spec/models/user_spec.rb describe User do it "should be invalid" do user = User.new(firstname: "really really ... super long firstname") expect(user.valid?).to_not eq true user = User.new(firstname: "sht") expect(user.valid?).to_not eq true end end False positive!!

Slide 16

Slide 16 text

# spec/models/user_spec.rb describe User do it "should be invalid" do user = User.new(firstname: "really really ... super long firstname") expect(user.valid?).to_not eq true user = User.new(firstname: "sht") expect(user.valid?).to_not eq true end end Mutating the User object and assessing if it’s valid, WHICH IT ISN’T, so what’s wrong?

Slide 17

Slide 17 text

# app/models/user.rb class User < ActiveRecord::Base has_secure_password validates :firstname, presence: true, length: 4..20 validates :middlename, length: 4..20, allow_blank: true validates :lastname, presence: true, length: 4..20 validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i } validates :password, presence: true, confirmation: true, length: { minimum: 8 } # ... end

Slide 18

Slide 18 text

# app/models/user.rb class User < ActiveRecord::Base has_secure_password validates :firstname, presence: true, length: 4..20 validates :middlename, length: 4..20, allow_blank: true validates :lastname, presence: true, length: 4..20 validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i } validates :password, presence: true, confirmation: true, length: { minimum: 8 } # ... end This validation IS failing, but…

Slide 19

Slide 19 text

# app/models/user.rb class User < ActiveRecord::Base has_secure_password validates :firstname, presence: true, length: 4..20 validates :middlename, length: 4..20, allow_blank: true validates :lastname, presence: true, length: 4..20 validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i } validates :password, presence: true, confirmation: true, length: { minimum: 8 } # ... end Other validations failing!! So are these…our test is passing, but it’s not actually passing for the right reasons.

Slide 20

Slide 20 text

# spec/models/user_spec.rb describe User do it "should be invalid" do user = User.new(firstname: "really really ... super long firstname") expect(user.valid?).to_not eq true user = User.new(firstname: "sht") expect(user.valid?).to_not eq true end end COMMUNICATION

Slide 21

Slide 21 text

# spec/models/user_spec.rb describe User do it "should be invalid" do user = User.new(firstname: "really really ... super long firstname") expect(user.valid?).to_not eq true user = User.new(firstname: "sht") expect(user.valid?).to_not eq true end end Conflicting communication: 1) be valid; and 2) to be true PROBLEM: We didn’t start with a valid object

Slide 22

Slide 22 text

describe User do # ... end RSpec#subject * default RSpec behavior is to initialize the described class (User) >> “User.new” * communicate the change by placing it at the top of the file (overriding the default) * NAME the subject. This communicates what it is. Don’t just leave “subject”.

Slide 23

Slide 23 text

describe User do subject(:user) { described_class.new } # ... end RSpec#subject * default RSpec behavior is to initialize the described class (User) >> “User.new” * communicate the change by placing it at the top of the file (overriding the default) * NAME the subject. This communicates what it is. Don’t just leave “subject”.

Slide 24

Slide 24 text

describe User do subject(:user) { described_class.new } # ... end RSpec#subject * default RSpec behavior is to initialize the described class (User) >> “User.new” * communicate the change by placing it at the top of the file (overriding the default) * NAME the subject. This communicates what it is. Don’t just leave “subject”.

Slide 25

Slide 25 text

describe User do subject(:user) { described_class.new } # ... end RSpec#subject * default RSpec behavior is to initialize the described class (User) >> “User.new” * communicate the change by placing it at the top of the file (overriding the default) * NAME the subject. This communicates what it is. Don’t just leave “subject”.

Slide 26

Slide 26 text

# spec/models/user_spec.rb describe User do it "should be invalid" do user = User.new(firstname: "really really ... super long firstname") expect(user.valid?).to_not eq true user = User.new(firstname: "sht") expect(user.valid?).to_not eq true end end Two different assertions: long name and short name, but in the same expectation block.

Slide 27

Slide 27 text

# spec/models/user_spec.rb describe User do it "should be invalid" do user = User.new(firstname: "really really ... super long firstname") expect(user.valid?).to_not eq true user = User.new(firstname: "sht") expect(user.valid?).to_not eq true end end Two different assertions: long name and short name, but in the same expectation block.

Slide 28

Slide 28 text

describe User do subject(:user) { described_class.new } # ... end Isolate each expectation in their own context (which loosely interpreted means “in this situation”)

Slide 29

Slide 29 text

describe User do subject(:user) { described_class.new } context 'with a firstname that is over 20 chars' do # ... end context 'with a firstname that is under 4 chars' do # ... end end Isolate each expectation in their own context (which loosely interpreted means “in this situation”)

Slide 30

Slide 30 text

describe User do subject(:user) { described_class.new } context 'with a firstname that is over 20 chars' do # ... end context 'with a firstname that is under 4 chars' do # ... end end Too Long Isolate each expectation in their own context (which loosely interpreted means “in this situation”)

Slide 31

Slide 31 text

describe User do subject(:user) { described_class.new } context 'with a firstname that is over 20 chars' do # ... end context 'with a firstname that is under 4 chars' do # ... end end Too Short Isolate each expectation in their own context (which loosely interpreted means “in this situation”)

Slide 32

Slide 32 text

describe User do subject(:user) { described_class.new } context 'with a firstname that is over 20 chars' do # ... end context 'with a firstname that is under 4 chars' do # ... end end Isolate each expectation in their own context (which loosely interpreted means “in this situation”)

Slide 33

Slide 33 text

describe User do subject(:user) { described_class.new firstname: firstname } context 'with a firstname that is over 20 chars' do let(:firstname) { 'really really ... super long firstname' } # ... end context 'with a firstname that is under 4 chars' do let(:firstname) { 'srt' } # ... end end Add the mutated value with let. If you’re not familiar with let or have used @instance vars in the past, I’ll explain the value at the end. Basics: let’s are lazy loaded (and cached), but allow you to override a parent example block

Slide 34

Slide 34 text

describe User do subject(:user) { described_class.new firstname: firstname } context 'with a firstname that is over 20 chars' do let(:firstname) { 'really really ... super long firstname' } # ... end context 'with a firstname that is under 4 chars' do let(:firstname) { 'srt' } # ... end end Add the mutated value with let. If you’re not familiar with let or have used @instance vars in the past, I’ll explain the value at the end. Basics: let’s are lazy loaded (and cached), but allow you to override a parent example block

Slide 35

Slide 35 text

# spec/models/user_spec.rb describe User do it "should be invalid" do user = User.new(firstname: "really really ... super long firstname") expect(user.valid?).to_not eq true user = User.new(firstname: "sht") expect(user.valid?).to_not eq true end end Two different assertions: long name and short name, but in the same expectation block.

Slide 36

Slide 36 text

# spec/models/user_spec.rb describe User do it "should be invalid" do user = User.new(firstname: "really really ... super long firstname") expect(user.valid?).to_not eq true user = User.new(firstname: "sht") expect(user.valid?).to_not eq true end end

Slide 37

Slide 37 text

# spec/models/user_spec.rb describe User do it "should be invalid" do user = User.new(firstname: "really really ... super long firstname") expect(user.valid?).to_not eq true user = User.new(firstname: "sht") expect(user.valid?).to_not eq true end end

Slide 38

Slide 38 text

expect(user.valid?).to_not eq true (true, not true?)

Slide 39

Slide 39 text

expect(user.valid?).to_not eq true (true, not true?)

Slide 40

Slide 40 text

RSpec Predicate Magic Methods

Slide 41

Slide 41 text

user.valid?

Slide 42

Slide 42 text

user.valid? expect(user).to_not be_valid

Slide 43

Slide 43 text

user.valid? expect(user).to_not be_valid

Slide 44

Slide 44 text

user.riding_a_monkey? expect(user).to_not be_riding_a_monkey

Slide 45

Slide 45 text

(true, not true?) expect(user).to_not be_valid RSpec has support for predicate methods: any predicate method on an object (with “?”) is automatically transposed in RSpec to a matcher.

Slide 46

Slide 46 text

(true?) expect(user).to be_invalid Rails has a method #invalid? which will help

Slide 47

Slide 47 text

(true?) expect(user).to be_invalid

Slide 48

Slide 48 text

describe User do subject(:user) { described_class.new firstname: firstname } context 'with a firstname that is over 20 chars' do let(:firstname) { 'really really ... super long firstname' } # ... end context 'with a firstname that is under 4 chars' do let(:firstname) { 'srt' } # ... end end

Slide 49

Slide 49 text

describe User do subject(:user) { described_class.new firstname: firstname } context 'with a firstname that is over 20 chars' do let(:firstname) { 'really really ... super long firstname' } specify { expect(user).to be_invalid } end context 'with a firstname that is under 4 chars' do let(:firstname) { 'srt' } specify { expect(user).to be_invalid } end end

Slide 50

Slide 50 text

describe User do subject(:user) { described_class.new firstname: firstname } context 'with a firstname that is over 20 chars' do let(:firstname) { 'really really ... super long firstname' } specify { expect(user).to be_invalid } end context 'with a firstname that is under 4 chars' do let(:firstname) { 'srt' } specify { expect(user).to be_invalid } end end Fix false positive ???

Slide 51

Slide 51 text

describe User do subject(:user) { described_class.new firstname: firstname } context 'with a firstname that is over 20 chars' do let(:firstname) { 'really really ... super long firstname' } specify { expect(user).to be_invalid } end context 'with a firstname that is under 4 chars' do let(:firstname) { 'srt' } specify { expect(user).to be_invalid } end end Still haven’t validated that it’s good, at first.

Slide 52

Slide 52 text

describe User do subject(:user) { described_class.new firstname: firstname } context 'with a firstname that is over 20 chars' do let(:firstname) { 'really really ... super long firstname' } specify { expect(user).to be_invalid } end context 'with a firstname that is under 4 chars' do let(:firstname) { 'srt' } specify { expect(user).to be_invalid } end end This is where the Minimum Valid comes in…

Slide 53

Slide 53 text

describe User do subject(:user) { described_class.new firstname: firstname } let(:firstname) { 'Adam' } specify { expect(user).to be_valid } context 'with a firstname that is over 20 chars' do let(:firstname) { 'really really ... super long firstname' } specify { expect(user).to be_invalid } end context 'with a firstname that is under 4 chars' do let(:firstname) { 'srt' } specify { expect(user).to be_invalid } end end Added the default/valid values for the object and assert that before we begin mutating values, that the parent object is in a valid state.

Slide 54

Slide 54 text

describe User do subject(:user) { described_class.new firstname: firstname } let(:firstname) { 'Adam' } specify { expect(user).to be_valid } context 'with a firstname that is over 20 chars' do let(:firstname) { 'really really ... super long firstname' } specify { expect(user).to be_invalid } end context 'with a firstname that is under 4 chars' do let(:firstname) { 'srt' } specify { expect(user).to be_invalid } end end ??

Slide 55

Slide 55 text

describe User do subject(:user) { described_class.new firstname: firstname } let(:firstname) { 'Adam' } specify { expect(user).to be_valid } context 'with a firstname that is over 20 chars' do let(:firstname) { 'really really ... super long firstname' } specify { expect(user).to be_invalid } end context 'with a firstname that is under 4 chars' do let(:firstname) { 'srt' } specify { expect(user).to be_invalid } end end

Slide 56

Slide 56 text

describe User do subject(:user) { described_class.new firstname: firstname } let(:firstname) { 'Adam' } specify { expect(user).to be_valid } context 'with a firstname that is over 20 chars' do let(:firstname) { 'really really ... super long firstname' } specify { expect(user).to be_invalid } end context 'with a firstname that is under 4 chars' do let(:firstname) { 'srt' } specify { expect(user).to be_invalid } end end ?? ??

Slide 57

Slide 57 text

describe User do subject(:user) { described_class.new firstname: firstname } let(:firstname) { 'Adam' } specify { expect(user).to be_valid } context 'with a firstname that is over 20 chars' do let(:firstname) { 'really really ... super long firstname' } specify { expect(user).to be_invalid } end context 'with a firstname that is under 4 chars' do let(:firstname) { 'srt' } specify { expect(user).to be_invalid } end end

Slide 58

Slide 58 text

describe User do subject(:user) { described_class.new firstname: firstname } let(:firstname) { 'Adam' } specify { expect(user).to be_valid } context 'with a firstname that is over 20 chars' do let(:firstname) { 'really really ... super long firstname' } specify { expect(user).to be_invalid } end context 'with a firstname that is under 4 chars' do let(:firstname) { 'srt' } specify { expect(user).to be_invalid } end end Fix false positive ??? When the validations run and it discovers the failed validations, it will catch those first. It’s a “linter” for our code.

Slide 59

Slide 59 text

describe User do subject(:user) { described_class.new firstname: firstname } let(:firstname) { 'Adam' } specify { expect(user).to be_valid } context 'with a firstname that is over 20 chars' do let(:firstname) { 'really really ... super long firstname' } specify { expect(user).to be_invalid } end context 'with a firstname that is under 4 chars' do let(:firstname) { 'srt' } specify { expect(user).to be_invalid } end end

Slide 60

Slide 60 text

describe User do subject(:user) { described_class.new firstname: firstname } let(:firstname) { 'Adam' } specify { expect(user).to be_valid } context 'with a firstname that is over 20 chars' do let(:firstname) { 'really really ... super long firstname' } specify { expect(user).to be_invalid } end context 'with a firstname that is under 4 chars' do let(:firstname) { 'srt' } specify { expect(user).to be_invalid } end end Love it or hate it, we can clean this up, as well

Slide 61

Slide 61 text

describe User do subject(:user) { described_class.new firstname: firstname } let(:firstname) { 'Adam' } it { is_expected.to be_valid } context 'with a firstname that is over 20 chars' do let(:firstname) { 'really really ... super long firstname' } it { is_expected.to be_invalid } end context 'with a firstname that is under 4 chars' do let(:firstname) { 'srt' } it { is_expected.to be_invalid } end end

Slide 62

Slide 62 text

describe User do subject(:user) { described_class.new firstname: firstname, middlename: middlename, lastname: lastname, email: email, password: password } let(:firstname) { 'Adam' } let(:middlename) { nil } let(:lastname) { 'Cuppy' } let(:email) { '[email protected]' } let(:password) { 'Passw0rd!' } it { is_expected.to be_valid } context 'with a firstname that is over 20 chars' do let(:firstname) { 'really really ... super long firstname' } it { is_expected.to be_invalid } end # ... end When we build out our MVO, it might look something like this…

Slide 63

Slide 63 text

describe User do subject(:user) { described_class.new firstname: firstname, middlename: middlename, lastname: lastname, email: email, password: password } let(:firstname) { 'Adam' } let(:middlename) { nil } let(:lastname) { 'Cuppy' } let(:email) { '[email protected]' } let(:password) { 'Passw0rd!' } it { is_expected.to be_valid } context 'with a firstname that is over 20 chars' do let(:firstname) { 'really really ... super long firstname' } it { is_expected.to be_invalid } end # ... end #1: Build the subject 1. build the MINIMUM valid object

Slide 64

Slide 64 text

describe User do subject(:user) { described_class.new firstname: firstname, middlename: middlename, lastname: lastname, email: email, password: password } let(:firstname) { 'Adam' } let(:middlename) { nil } let(:lastname) { 'Cuppy' } let(:email) { '[email protected]' } let(:password) { 'Passw0rd!' } it { is_expected.to be_valid } context 'with a firstname that is over 20 chars' do let(:firstname) { 'really really ... super long firstname' } it { is_expected.to be_invalid } end # ... end #2: Set the config values 1. Include all the configurable values in their min required state (ex: middlename) to communicate what’s part of a valid object. Long spec? This helps resolve what can change.

Slide 65

Slide 65 text

describe User do subject(:user) { described_class.new firstname: firstname, middlename: middlename, lastname: lastname, email: email, password: password } let(:firstname) { 'Adam' } let(:middlename) { nil } let(:lastname) { 'Cuppy' } let(:email) { '[email protected]' } let(:password) { 'Passw0rd!' } it { is_expected.to be_valid } context 'with a firstname that is over 20 chars' do let(:firstname) { 'really really ... super long firstname' } it { is_expected.to be_invalid } end # ... end #3 Assert a Valid state

Slide 66

Slide 66 text

describe User do subject(:user) { described_class.new firstname: firstname, middlename: middlename, lastname: lastname, email: email, password: password } let(:firstname) { 'Adam' } let(:middlename) { nil } let(:lastname) { 'Cuppy' } let(:email) { '[email protected]' } let(:password) { 'Passw0rd!' } it { is_expected.to be_valid } context 'with a firstname that is over 20 chars' do let(:firstname) { 'really really ... super long firstname' } it { is_expected.to be_invalid } end # ... end #4 Mutate Mutate the config value with a greater degree of CONFIDENCE that what’s changes is related to a potential failure

Slide 67

Slide 67 text

describe User do subject(:user) { described_class.new firstname: firstname, middlename: middlename, lastname: lastname, email: email, password: password } let(:firstname) { 'Adam' } let(:middlename) { nil } let(:lastname) { 'Cuppy' } let(:email) { '[email protected]' } let(:password) { 'Passw0rd!' } it { is_expected.to be_valid } context 'with a firstname that is over 20 chars' do let(:firstname) { 'really really ... super long firstname' } it { is_expected.to be_invalid } end # ... end

Slide 68

Slide 68 text

Permutation Tables

Slide 69

Slide 69 text

1. Define sets of data 2. Define the output of each set 3. Assert the method creates the output from the data (input/output) P.T. Workflow…

Slide 70

Slide 70 text

class User < ActiveRecord::Base # ... def fullname [firstname, middlename, lastname].compact.join ' ' end end

Slide 71

Slide 71 text

class User < ActiveRecord::Base # ... def fullname [firstname, middlename, lastname].compact.join ' ' end end

Slide 72

Slide 72 text

class User < ActiveRecord::Base # ... def fullname [firstname, middlename, lastname].compact.join ' ' end end

Slide 73

Slide 73 text

class User < ActiveRecord::Base # ... def fullname [firstname, middlename, lastname].compact.join ' ' end end

Slide 74

Slide 74 text

describe User do # ... describe ‘#fullname' do subject(:fullname) { user.fullname } context 'when firstname is blank' do let(:firstname) { nil } it { is_expected.to eq lastname } end context 'when lastname is blank' do let(:lastname) { nil } it { is_expected.to eq firstname } end context 'when first and lastname are blank' do let(:firstname) { nil } let(:lastname) { nil } it { is_expected.to be_blank } end context 'when there are all three names' do let(:middlename) { 'Jimmy' } it { is_expected.to eq "#{firstname} #{middlename} #{lastname}" } end end # ... end Following an MVO, our spec may end up a little like this…

Slide 75

Slide 75 text

describe User do # ... describe ‘#fullname' do subject(:fullname) { user.fullname } context 'when firstname is blank' do let(:firstname) { nil } it { is_expected.to eq lastname } end context 'when lastname is blank' do let(:lastname) { nil } it { is_expected.to eq firstname } end context 'when first and lastname are blank' do let(:firstname) { nil } let(:lastname) { nil } it { is_expected.to be_blank } end context 'when there are all three names' do let(:middlename) { 'Jimmy' } it { is_expected.to eq "#{firstname} #{middlename} #{lastname}" } end end # ... end

Slide 76

Slide 76 text

describe '#fullname' do subject(:fullname) { user.fullname } # ... end

Slide 77

Slide 77 text

describe User do # ... describe ‘#fullname' do subject(:fullname) { user.fullname } context 'when firstname is blank' do let(:firstname) { nil } it { is_expected.to eq lastname } end context 'when lastname is blank' do let(:lastname) { nil } it { is_expected.to eq firstname } end context 'when first and lastname are blank' do let(:firstname) { nil } let(:lastname) { nil } it { is_expected.to be_blank } end context 'when there are all three names' do let(:middlename) { 'Jimmy' } it { is_expected.to eq "#{firstname} #{middlename} #{lastname}" } end end # ... end

Slide 78

Slide 78 text

describe User do # ... describe ‘#fullname' do subject(:fullname) { user.fullname } context 'when firstname is blank' do let(:firstname) { nil } it { is_expected.to eq lastname } end context 'when lastname is blank' do let(:lastname) { nil } it { is_expected.to eq firstname } end context 'when first and lastname are blank' do let(:firstname) { nil } let(:lastname) { nil } it { is_expected.to be_blank } end context 'when there are all three names' do let(:middlename) { 'Jimmy' } it { is_expected.to eq "#{firstname} #{middlename} #{lastname}" } end end # ... end

Slide 79

Slide 79 text

describe User do # ... describe ‘#fullname' do subject(:fullname) { user.fullname } context 'when firstname is blank' do let(:firstname) { nil } it { is_expected.to eq lastname } end context 'when lastname is blank' do let(:lastname) { nil } it { is_expected.to eq firstname } end context 'when first and lastname are blank' do let(:firstname) { nil } let(:lastname) { nil } it { is_expected.to be_blank } end context 'when there are all three names' do let(:middlename) { 'Jimmy' } it { is_expected.to eq "#{firstname} #{middlename} #{lastname}" } end end # ... end Anything missing ??

Slide 80

Slide 80 text

describe User do # ... describe ‘#fullname' do subject(:fullname) { user.fullname } context 'when firstname is blank' do let(:firstname) { nil } it { is_expected.to eq lastname } end context 'when lastname is blank' do let(:lastname) { nil } it { is_expected.to eq firstname } end context 'when first and lastname are blank' do let(:firstname) { nil } let(:lastname) { nil } it { is_expected.to be_blank } end context 'when there are all three names' do let(:middlename) { 'Jimmy' } it { is_expected.to eq "#{firstname} #{middlename} #{lastname}" } end end # ... end

Slide 81

Slide 81 text

describe ‘#fullname' do subject(:fullname) { user.fullname } context 'when firstname is blank' do let(:firstname) { nil } it { is_expected.to eq lastname } end #... end

Slide 82

Slide 82 text

describe ‘#fullname' do subject(:fullname) { user.fullname } context 'when firstname is blank' do let(:firstname) { nil } it { is_expected.to eq lastname } end #... end

Slide 83

Slide 83 text

describe ‘#fullname' do subject(:fullname) { user.fullname } context 'when firstname is blank' do let(:firstname) { nil } it { is_expected.to eq lastname } end #... end The test doesn’t tell us how the two are related << COMMUNICATION BROKEN!!

Slide 84

Slide 84 text

describe ‘#fullname' do # context 'when firstname is blank' do # let(:firstname) { nil } # it { is_expected.to eq lastname } # end { [nil, 'James', 'Johnson'] => 'James Johnson', }.each do |name_set, output| # ... end end Firstname Introduce a PERMUTATION TABLE * We see the player in the game (first, mid, last) * And, the result of those players (mid, last)

Slide 85

Slide 85 text

describe ‘#fullname' do # context 'when firstname is blank' do # let(:firstname) { nil } # it { is_expected.to eq lastname } # end { [nil, 'James', 'Johnson'] => 'James Johnson', }.each do |name_set, output| # ... end end middlename Introduce a PERMUTATION TABLE * We see the player in the game (first, mid, last) * And, the result of those players (mid, last)

Slide 86

Slide 86 text

describe ‘#fullname' do # context 'when firstname is blank' do # let(:firstname) { nil } # it { is_expected.to eq lastname } # end { [nil, 'James', 'Johnson'] => 'James Johnson', }.each do |name_set, output| # ... end end Lastname Introduce a PERMUTATION TABLE * We see the player in the game (first, mid, last) * And, the result of those players (mid, last)

Slide 87

Slide 87 text

describe ‘#fullname' do # context 'when firstname is blank' do # let(:firstname) { nil } # it { is_expected.to eq lastname } # end { [nil, 'James', 'Johnson'] => 'James Johnson', }.each do |name_set, output| # ... end end Fullname Introduce a PERMUTATION TABLE * We see the player in the game (first, mid, last) * And, the result of those players (mid, last)

Slide 88

Slide 88 text

describe ‘#fullname' do # ... { [nil, 'James', 'Johnson'] => 'James Johnson', [nil, 'James', nil] => 'James', ['Jimmy', 'James', nil] => 'Jimmy James', ['Jimmy', 'James', 'Johnson'] => 'Jimmy James Johnson' }.each do |name_set, output| # ... end end I can visually see what’s missing now…

Slide 89

Slide 89 text

describe ‘#fullname' do # ... { [nil, 'James', 'Johnson'] => 'James Johnson', [nil, 'James', nil] => 'James', ['Jimmy', 'James', nil] => 'Jimmy James', ['Jimmy', 'James', 'Johnson'] => 'Jimmy James Johnson' }.each do |name_set, output| # ... end end Anything missing ?? I can visually see what’s missing now…

Slide 90

Slide 90 text

describe ‘#fullname' do # ... { [nil, 'James', 'Johnson'] => 'James Johnson', [nil, 'James', nil] => 'James', ['Jimmy', 'James', nil] => 'Jimmy James', ['Jimmy', 'James', 'Johnson'] => 'Jimmy James Johnson' ['Jimmy', nil, nil] => 'Jimmy', [nil, nil, 'Johnson'] => 'Johnson', ['Jimmy', nil, 'Johnson'] => 'Jimmy Johnson' }.each do |name_set, output| # ... end end so, I can fill in the gaps

Slide 91

Slide 91 text

describe ‘#fullname' do shared_examples_for 'a fullname' do |(first, middle, last), output| subject(:fullname) { user.fullname } let(:firstname) { first } let(:middlename) { middle } let(:lastname) { last } it { is_expected.to eq output } end { [nil, 'James', 'Johnson'] => 'James Johnson', [nil, 'James', nil] => 'James', ['Jimmy', 'James', nil] => 'Jimmy James', ['Jimmy', 'James', 'Johnson'] => 'Jimmy James Johnson' ['Jimmy', nil, nil] => 'Jimmy', [nil, nil, 'Johnson'] => 'Johnson', ['Jimmy', nil, 'Johnson'] => 'Jimmy Johnson' }.each do |name_set, output| it_behaves_like 'a fullname', name_set, output end end

Slide 92

Slide 92 text

describe ‘#fullname' do shared_examples_for 'a fullname' do |(first, middle, last), output| subject(:fullname) { user.fullname } let(:firstname) { first } let(:middlename) { middle } let(:lastname) { last } it { is_expected.to eq output } end { [nil, 'James', 'Johnson'] => 'James Johnson', [nil, 'James', nil] => 'James', ['Jimmy', 'James', nil] => 'Jimmy James', ['Jimmy', 'James', 'Johnson'] => 'Jimmy James Johnson' ['Jimmy', nil, nil] => 'Jimmy', [nil, nil, 'Johnson'] => 'Johnson', ['Jimmy', nil, 'Johnson'] => 'Jimmy Johnson' }.each do |name_set, output| it_behaves_like 'a fullname', name_set, output end end * Move our common code to a shared example that unpacks the segments and compares to the output * Gotcha: DON’T back too much logic into this. We don’t want to test the test

Slide 93

Slide 93 text

describe ‘#fullname' do shared_examples_for 'a fullname' do |(first, middle, last), output| subject(:fullname) { user.fullname } let(:firstname) { first } let(:middlename) { middle } let(:lastname) { last } it { is_expected.to eq output } end { [nil, 'James', 'Johnson'] => 'James Johnson', [nil, 'James', nil] => 'James', ['Jimmy', 'James', nil] => 'Jimmy James', ['Jimmy', 'James', 'Johnson'] => 'Jimmy James Johnson' ['Jimmy', nil, nil] => 'Jimmy', [nil, nil, 'Johnson'] => 'Johnson', ['Jimmy', nil, 'Johnson'] => 'Jimmy Johnson' }.each do |name_set, output| it_behaves_like 'a fullname', name_set, output end end

Slide 94

Slide 94 text

describe ‘#fullname' do shared_examples_for 'a fullname' do |(first, middle, last), output| subject(:fullname) { user.fullname } let(:firstname) { first } let(:middlename) { middle } let(:lastname) { last } it { is_expected.to eq output } end { [nil, 'James', 'Johnson'] => 'James Johnson', [nil, 'James', nil] => 'James', ['Jimmy', 'James', nil] => 'Jimmy James', ['Jimmy', 'James', 'Johnson'] => 'Jimmy James Johnson' ['Jimmy', nil, nil] => 'Jimmy', [nil, nil, 'Johnson'] => 'Johnson', ['Jimmy', nil, 'Johnson'] => 'Jimmy Johnson' }.each do |name_set, output| it_behaves_like 'a fullname', name_set, output end end

Slide 95

Slide 95 text

Golden Master

Slide 96

Slide 96 text

1. Backfilling untested legacy code 2. Uncertain expectations require visual confirmation 3. Code complexity significantly exceeds current domain knowledge Golden Master Testing

Slide 97

Slide 97 text

require 'rails_helper' RSpec.describe User, type: :model do pending "add some examples to (or delete) #{__FILE__}" end This sucks to find

Slide 98

Slide 98 text

1. Take a snapshot of an object (to a file) 2. Verify the snapshot (manually) 3. Compare future versions to the verified “master” Golden Master Testing Workflow… 3.

Slide 99

Slide 99 text

https://github.com/kytrinyx/approvals

Slide 100

Slide 100 text

# spec/spec_helper.rb require ‘approvals/rspec' # path/to/spec.rb it 'works' do verify format: :html do "

ZOMG

" end end # Manually verify snapshots $ cd /path/to/app $ approvals verify https://github.com/kytrinyx/approvals

Slide 101

Slide 101 text

# spec/spec_helper.rb require ‘approvals/rspec' # path/to/spec.rb it 'works' do verify format: :html do "

ZOMG

" end end # Manually verify snapshots $ cd /path/to/app $ approvals verify https://github.com/kytrinyx/approvals * Captures an output (could be a method’s output) * Write it to a file (appended “received”)

Slide 102

Slide 102 text

# spec/spec_helper.rb require ‘approvals/rspec' # path/to/spec.rb it 'works' do verify format: :html do "

ZOMG

" end end # Manually verify snapshots $ cd /path/to/app $ approvals verify https://github.com/kytrinyx/approvals * steps through “received” snapshots * allow you to manually verify the output (appends with “approved”) * saves it for later comparison

Slide 103

Slide 103 text

Best Practices

Slide 104

Slide 104 text

Best Practices Better

Slide 105

Slide 105 text

Best Practices Better Good, but not Required

Slide 106

Slide 106 text

Best Practices Better Good, but not Required Other ways of doing things, and why it might be helpfuL…ideas

Slide 107

Slide 107 text

Best Practices Better Good, but not Required Other ways of doing things, and why it might be helpfuL…ideas

Slide 108

Slide 108 text

let (not @instance vars)

Slide 109

Slide 109 text

@fullname = 'Adam Cuppy' specify { expect(@fullnmae).to eq 'Adam Cuppy' }

Slide 110

Slide 110 text

@fullname = 'Adam Cuppy' specify { expect(@fullnmae).to eq 'Adam Cuppy' } # => expected: "Adam Cuppy", got: nil ???

Slide 111

Slide 111 text

@fullname = 'Adam Cuppy' specify { expect(@fullnmae).to eq 'Adam Cuppy' }

Slide 112

Slide 112 text

@fullname = 'Adam Cuppy' specify { expect(@fullnmae).to eq 'Adam Cuppy' } !!!

Slide 113

Slide 113 text

let(:fullname) { 'Adam Cuppy' } specify { expect(fullnmae).to eq 'Adam Cuppy' }

Slide 114

Slide 114 text

let(:fullname) { 'Adam Cuppy' } specify { expect(fullnmae).to eq 'Adam Cuppy' } # => NameError: undefined local variable or method `fullnmae'

Slide 115

Slide 115 text

Descriptive Naming

Slide 116

Slide 116 text

it { expect(user1).to be_valid } it { expect(user2).to be_invalid }

Slide 117

Slide 117 text

it { expect(user).to be_valid } it { expect(user_with_duplicate_email).to be_invalid }

Slide 118

Slide 118 text

describe 'validations' do subject(:user) { User.new email: '[email protected]' } it { is_expected.to be_valid } context 'when an email is used' do let(:user_with_duplicate_email) do User.new email: user.email end it { expect(user_with_duplicate_email).to be_invalid } end end

Slide 119

Slide 119 text

describe 'validations' do subject(:user) { User.new email: '[email protected]' } it { is_expected.to be_valid } context 'when an email is used' do let(:user_with_duplicate_email) do User.new email: user.email end it { expect(user_with_duplicate_email).to be_invalid } end end

Slide 120

Slide 120 text

describe 'validations' do subject(:user) { User.new email: '[email protected]' } it { is_expected.to be_valid } context 'when an email is used' do let(:user_with_duplicate_email) do User.new email: user.email end it { expect(user_with_duplicate_email).to be_invalid } end end

Slide 121

Slide 121 text

describe 'validations' do subject(:user) { User.new email: '[email protected]' } it { is_expected.to be_valid } context 'when an email is used' do let(:user_with_duplicate_email) do User.new email: user.email end it { expect(user_with_duplicate_email).to be_invalid } end end

Slide 122

Slide 122 text

Extract Common Expectations

Slide 123

Slide 123 text

Custom Matchers

Slide 124

Slide 124 text

require 'rspec/expectations' RSpec::Matchers.define :be_a_multiple_of do |expected| match do |actual| actual % expected == 0 end end RSpec.describe 9 do it { is_expected.to be_a_multiple_of(3) } end https://www.relishapp.com/rspec/rspec-expectations/v/3-3/docs/custom-matchers/define-matcher

Slide 125

Slide 125 text

require 'rspec/expectations' RSpec::Matchers.define :be_a_multiple_of do |expected| match do |actual| actual % expected == 0 end end RSpec.describe 9 do it { is_expected.to be_a_multiple_of(3) } end https://www.relishapp.com/rspec/rspec-expectations/v/3-3/docs/custom-matchers/define-matcher

Slide 126

Slide 126 text

Factories over Fixtures

Slide 127

Slide 127 text

https://github.com/thoughtbot/factory_girl

Slide 128

Slide 128 text

describe User do subject(:user) { User.new } # ... end

Slide 129

Slide 129 text

describe User do subject(:user) { FactoryGirl.build :user } # ... end

Slide 130

Slide 130 text

describe User do subject(:user) { FactoryGirl.build :user } # ... end * Allow your to build Minimum Valid Objects

Slide 131

Slide 131 text

FactoryGirl.lint! * Validates your ActiveRecord objects are valid

Slide 132

Slide 132 text

https://github.com/thoughtbot/factory_girl * Downside: hides a lot of model construction, so I find that it starts small and grows fast (hiding a lot)

Slide 133

Slide 133 text

Readables

Slide 134

Slide 134 text

http://betterspecs.org/

Slide 135

Slide 135 text

Randy Coulman - http://randycoulman.com/blog/categories/getting-testy/

Slide 136

Slide 136 text

Randy Coulman - http://randycoulman.com/blog/categories/getting-testy/

Slide 137

Slide 137 text

Sandi Metz - http://www.poodr.com

Slide 138

Slide 138 text

https://codingzeal.com/

Slide 139

Slide 139 text

www.codingzeal.com @adamcuppy Adam Cuppy