Slide 1

Slide 1 text

Sam Phippen How RSpec Works

Slide 2

Slide 2 text

Sam Phippen @samphippen Is an RSpec lead maintainer And works at Google

Slide 3

Slide 3 text

Sam Phippen Any code (except code from RSpec itself) in this talk is copyright Google under Apache V2

Slide 4

Slide 4 text

Sam Phippen Matz Hates Tests

Slide 5

Slide 5 text

Sam Phippen So RSpec is cancelled

Slide 6

Slide 6 text

Sam Phippen Thanks @samphippen [email protected]

Slide 7

Slide 7 text

Sam Phippen Sam Phippen

Slide 8

Slide 8 text

Sam Phippen Sam Phippen

Slide 9

Slide 9 text

Sam Phippen gem “rspec”, “~> 3.8”

Slide 10

Slide 10 text

Sam Phippen specs: diff-lcs (1.3) rspec (3.8.0) rspec-core (~> 3.8.0) rspec-expectations (~> 3.8.0) rspec-mocks (~> 3.8.0) rspec-core (3.8.0) rspec-support (~> 3.8.0)

Slide 11

Slide 11 text

Sam Phippen rspec rspec-core rspec-mocks rspec-expectations

Slide 12

Slide 12 text

Sam Phippen rspec Meta gem that provides the other gems

Slide 13

Slide 13 text

Sam Phippen rspec-core describe, it, runner, let, etc

Slide 14

Slide 14 text

Sam Phippen rspec-mocks allow, double, to receive, etc

Slide 15

Slide 15 text

Sam Phippen rspec-expectations expect keyword and matchers

Slide 16

Slide 16 text

Sam Phippen rspec-support Internal code required to make rspec work

Slide 17

Slide 17 text

Sam Phippen gem “rspec-rails”, “~> 3.8”

Slide 18

Slide 18 text

Sam Phippen rspec-rails rspec rspec-mocks rspec-core rspec-expectations Rails specific code

Slide 19

Slide 19 text

Sam Phippen gem “rspec” gem “rspec-rails” !

Slide 20

Slide 20 text

Sam Phippen So, how does RSpec work?

Slide 21

Slide 21 text

Sam Phippen RSpec.describe "calculation" do it "can add numbers" do expect(1 + 1).to eq(2) end end

Slide 22

Slide 22 text

Sam Phippen RSpec.describe "calculation" do it "can add numbers" do expect(1 + 1).to eq(2) end end

Slide 23

Slide 23 text

Sam Phippen Example Group l/r-c/ example_group.rb

Slide 24

Slide 24 text

Sam Phippen bundle exec rspec

Slide 25

Slide 25 text

Sam Phippen Runner

Slide 26

Slide 26 text

Sam Phippen World Runner

Slide 27

Slide 27 text

Sam Phippen l/r-c/runner.rb l/r-c/world.rb

Slide 28

Slide 28 text

Sam Phippen —pattern=spec/**/*_spec.rb

Slide 29

Slide 29 text

Sam Phippen World Runner ExampleGroup ExampleGroup ExampleGroup

Slide 30

Slide 30 text

Sam Phippen —tag=foo (e.g describe, “…”, :foo do) —example=“calculates 4+4”

Slide 31

Slide 31 text

Sam Phippen Runner

Slide 32

Slide 32 text

Sam Phippen Runner Configuration Options

Slide 33

Slide 33 text

Sam Phippen Runner Configuration Options FilterManager

Slide 34

Slide 34 text

Sam Phippen World ExampleGroup ExampleGroup ExampleGroup FilterManager

Slide 35

Slide 35 text

Sam Phippen World ExampleGroup ExampleGroup ExampleGroup FilterManager

Slide 36

Slide 36 text

Sam Phippen World contains many ExampleGroups ready for execution

Slide 37

Slide 37 text

Sam Phippen RSpec.describe "calculation" do it "can add numbers" do expect(1 + 1).to eq(2) end end

Slide 38

Slide 38 text

Sam Phippen Example l/r-c/ example.rb

Slide 39

Slide 39 text

Sam Phippen A “test”

Slide 40

Slide 40 text

Sam Phippen RSpec.describe "eg1" do it "e1" it "e2" context "eg1_1" do it "e3" end context "eg2_1" do it "e3" end end

Slide 41

Slide 41 text

Sam Phippen EG EG E E EG E E E E

Slide 42

Slide 42 text

Sam Phippen So what about randomization?

Slide 43

Slide 43 text

Sam Phippen E EG E EG EG E E

Slide 44

Slide 44 text

Sam Phippen RSpec.describe "eg1" do before {} after {} around { |ex| ex.run} it "e1" end

Slide 45

Slide 45 text

Sam Phippen RSpec.describe "eg1" do before {} after {} around { |ex| ex.run} it "e1" end

Slide 46

Slide 46 text

Sam Phippen Hooks
 l/r-c/ hooks.rb

Slide 47

Slide 47 text

Sam Phippen Execute once before(:suite) after(:suite)

Slide 48

Slide 48 text

Sam Phippen Execute once per example group before(:context) after(:context)

Slide 49

Slide 49 text

Sam Phippen Execute once per example before after around

Slide 50

Slide 50 text

Sam Phippen EG E BS BC B E B A A

Slide 51

Slide 51 text

Sam Phippen EG E BC B A AC B

Slide 52

Slide 52 text

Sam Phippen RSpec core is only responsible for the structure and execution of your test suite

Slide 53

Slide 53 text

Sam Phippen RSpec.describe "calculation" do it "can add numbers" do expect(1 + 1).to eq(2) end end

Slide 54

Slide 54 text

Sam Phippen Expectation Provided by rspec-expectations

Slide 55

Slide 55 text

Sam Phippen RSpec.describe "calculation" do it "can add numbers" do expect(1 + 1).to eq(2) end end

Slide 56

Slide 56 text

Sam Phippen ExpectationTarget Matcher

Slide 57

Slide 57 text

Sam Phippen ExpectationTarget Matcher

Slide 58

Slide 58 text

Sam Phippen expect(…).to

Slide 59

Slide 59 text

Sam Phippen def to(matcher=nil, message=nil, &block) RSpec::Expectations:: PositiveExpectationHandler .handle_matcher(target, matcher, message, &block) end

Slide 60

Slide 60 text

Sam Phippen expect(…).to_not expect(…).not_to

Slide 61

Slide 61 text

Sam Phippen def not_to(matcher=nil, message=nil, &block) RSpec::Expectations:: NegativeExpectationHandler .handle_matcher(target, matcher, message, &block) end

Slide 62

Slide 62 text

Sam Phippen ExpectationTarget wraps a user object for matching

Slide 63

Slide 63 text

Sam Phippen RSpec.describe "calculation" do it "can add numbers" do expect(1 + 1).to eq(2) end end

Slide 64

Slide 64 text

Sam Phippen ExpectationTarget Matcher

Slide 65

Slide 65 text

Sam Phippen def eq(expected) BuiltIn::Eq .new(expected) end

Slide 66

Slide 66 text

Sam Phippen class Eq < BaseMatcher def match(expected, actual) actual == expected end end

Slide 67

Slide 67 text

Sam Phippen class BaseMatcher include RSpec::Matchers::Composable def matches?(actual) @actual = actual match(expected, actual) end end

Slide 68

Slide 68 text

Sam Phippen RSpec.describe "calculation" do it "can add numbers" do expect(1 + 1).to eq(2) end end

Slide 69

Slide 69 text

Sam Phippen Composable

Slide 70

Slide 70 text

Sam Phippen expect(…).to match([ a_kind_of(String), a_hash_including(a: 4) ])

Slide 71

Slide 71 text

Sam Phippen [ “any string”, {a: 4, b: 6.0}, ]

Slide 72

Slide 72 text

Sam Phippen class Match < BaseMatcher def match(expected, actual) return true if values_match?( expected, actual) actual.match(expected) end end

Slide 73

Slide 73 text

Sam Phippen class Match < BaseMatcher def match(expected, actual) return true if values_match?( expected, actual) actual.match(expected) end end

Slide 74

Slide 74 text

Sam Phippen def values_match?(expected, actual) expected = with_matchers_cloned(expected) Support::FuzzyMatcher.values_match?( expected, actual) end

Slide 75

Slide 75 text

Sam Phippen RSpec support is shared code between RSpec libraries

Slide 76

Slide 76 text

Sam Phippen def self.values_match?(expected, actual) if Hash === actual return hashes_match?(expected, actual) if Hash === expected elsif Array === expected && Enumerable === actual && !(Struct === actual) return arrays_match?(expected, actual.to_a) end return true if expected == actual begin expected === actual rescue ArgumentError # Some objects, like 0-arg lambdas on 1.9+, raise # ArgumentError for `expected === actual`. false end end

Slide 77

Slide 77 text

Sam Phippen You can’t read this.

Slide 78

Slide 78 text

Sam Phippen def self.values_match?(e, a) if Hash === a return hashes_match?(e, a) if Hash === e elsif Array === e && Enumerable === a && !(Struct === a) return arrays_match?( e, a) end

Slide 79

Slide 79 text

Sam Phippen def self.values_match?(e, a) if Hash === a return hashes_match?(e, a) if Hash === e elsif Array === e && Enumerable === a && !(Struct === a) return arrays_match?( e, a) end

Slide 80

Slide 80 text

Sam Phippen def self.hashes_match?( expected_hash, actual_hash) return false if expected_hash.size != actual_hash.size expected_hash.all? do |e_k, e_v| actual_value = actual_hash.fetch(e_k) { return false } values_match?(e_v, actual_value) end end

Slide 81

Slide 81 text

Sam Phippen def self.hashes_match?( expected_hash, actual_hash) return false if expected_hash.size != actual_hash.size expected_hash.all? do |e_k, e_v| actual_value = actual_hash.fetch(e_k) { return false } values_match?(e_v, actual_value) end end

Slide 82

Slide 82 text

Sam Phippen def self.hashes_match?( expected_hash, actual_hash) return false if expected_hash.size != actual_hash.size expected_hash.all? do |e_k, e_v| actual_value = actual_hash.fetch(e_k) { return false } values_match?(e_v, actual_value) end end

Slide 83

Slide 83 text

Sam Phippen def self.hashes_match?( expected_hash, actual_hash) return false if expected_hash.size != actual_hash.size expected_hash.all? do |e_k, e_v| actual_value = actual_hash.fetch(e_k) { return false } values_match?(e_v, actual_value) end end

Slide 84

Slide 84 text

Sam Phippen def self.hashes_match?( expected_hash, actual_hash) return false if expected_hash.size != actual_hash.size expected_hash.all? do |e_k, e_v| actual_value = actual_hash.fetch(e_k) { return false } values_match?(e_v, actual_value) end end

Slide 85

Slide 85 text

Sam Phippen def self.values_match?(e, a) if Hash === a return hashes_match?(e, a) if Hash === e elsif Array === e && Enumerable === a && !(Struct === a) return arrays_match?( e, a) end

Slide 86

Slide 86 text

Sam Phippen def self.arrays_match?( expected_list, actual_list) return false if expected_list.size!=actual_list.size expected_list.zip(actual_list).all? do |expected, actual| values_match?(expected, actual) end end

Slide 87

Slide 87 text

Sam Phippen def self.arrays_match?( expected_list, actual_list) return false if expected_list.size!=actual_list.size expected_list.zip(actual_list).all? do |expected, actual| values_match?(expected, actual) end end

Slide 88

Slide 88 text

Sam Phippen def self.arrays_match?( expected_list, actual_list) return false if expected_list.size!=actual_list.size expected_list.zip(actual_list).all? do |expected, actual| values_match?(expected, actual) end end

Slide 89

Slide 89 text

Sam Phippen RSpec recurses array and hash data structures, doing matches.

Slide 90

Slide 90 text

Sam Phippen def values_match(expected, actual) … (the code we saw earlier) return true if expected == actual begin expected === actual rescue ArgumentError # Some objects, like 0-arg lambdas on # 1.9+, raise # ArgumentError for `expected === actual`. false end end

Slide 91

Slide 91 text

Sam Phippen def values_match(expected, actual) … (the code we saw earlier) return true if expected == actual begin expected === actual rescue ArgumentError # Some objects, like 0-arg lambdas on # 1.9+, raise # ArgumentError for `expected === actual`. false end end

Slide 92

Slide 92 text

Sam Phippen def values_match(expected, actual) … (the code we saw earlier) return true if expected == actual begin expected === actual rescue ArgumentError # Some objects, like 0-arg lambdas on # 1.9+, raise # ArgumentError for `expected === actual`. false end end

Slide 93

Slide 93 text

Sam Phippen /a/ === “a” lambda { |x| x == 1 } === 1 Any Rspec matcher === a value it matches

Slide 94

Slide 94 text

Sam Phippen RSpec aliases === to matches? On matchers

Slide 95

Slide 95 text

Sam Phippen So let’s put it all back together

Slide 96

Slide 96 text

Sam Phippen expect(…).to match([ a_kind_of(String), a_hash_including(a: 4) ])

Slide 97

Slide 97 text

Sam Phippen [ “any string”, {a: 4, b: 6.0}, ]

Slide 98

Slide 98 text

Sam Phippen Call arrays_match?

Slide 99

Slide 99 text

Sam Phippen [ “any string”, {a: 4, b: 6.0}, ]

Slide 100

Slide 100 text

Sam Phippen Call === on “a_kind_of(String)”

Slide 101

Slide 101 text

Sam Phippen [ “any string”, ✅ {a: 4, b: 6.0}, ]

Slide 102

Slide 102 text

Sam Phippen Call === on a_hash_including?(a: 4)

Slide 103

Slide 103 text

Sam Phippen [ “any string”, ✅ {a: 4, b: 6.0}, ✅ ]

Slide 104

Slide 104 text

Sam Phippen Match returns true

Slide 105

Slide 105 text

Sam Phippen RSpec expectations contains expect, and matching logic.

Slide 106

Slide 106 text

Sam Phippen rspec-mocks

Slide 107

Slide 107 text

Sam Phippen RSpec.describe "mocking" do it “adds a foo method" do allow(foo).to receive(:bees) expect(foo.bees).to eq(nil) end end

Slide 108

Slide 108 text

Sam Phippen RSpec.describe "mocking" do it “adds a foo method" do allow(foo).to receive(:bees) expect(foo.bees).to eq(nil) end end

Slide 109

Slide 109 text

Sam Phippen AllowanceTarget

Slide 110

Slide 110 text

Sam Phippen AllowanceTarget TargetBase

Slide 111

Slide 111 text

Sam Phippen AllowanceTarget calls #delegate_to with :setup_allowance on TargetBase

Slide 112

Slide 112 text

Sam Phippen expect(foo).to receive

Slide 113

Slide 113 text

Sam Phippen ExpectationTarget calls #delegate_to with :setup_expectation on TargetBase

Slide 114

Slide 114 text

Sam Phippen AllowanceTarget TargetBase

Slide 115

Slide 115 text

Sam Phippen TargetBase defines #to on AllowanceTarget

Slide 116

Slide 116 text

Sam Phippen The defined #to calls :setup_allowance on matchers passed to #to

Slide 117

Slide 117 text

Sam Phippen This meta programming step allows deduplication with ExpectationTarget

Slide 118

Slide 118 text

Sam Phippen AllowanceTarget TargetBase

Slide 119

Slide 119 text

Sam Phippen RSpec.describe "mocking" do it “adds a foo method" do allow(foo).to receive(:bees) expect(foo.bees).to eq(nil) end end

Slide 120

Slide 120 text

Sam Phippen Receive matcher handles stubbing and mocking for RSpec

Slide 121

Slide 121 text

Sam Phippen AllowanceTarget#to calls setup_allowance on the passed matcher

Slide 122

Slide 122 text

Sam Phippen AllowanceTarget TargetBase Receive

Slide 123

Slide 123 text

Sam Phippen Receive#setup_allowance creates a Proxy for object

Slide 124

Slide 124 text

Sam Phippen AllowanceTarget TargetBase Receive Proxy

Slide 125

Slide 125 text

Sam Phippen Receive#setup_allowance Calls #add_stub on the Proxy

Slide 126

Slide 126 text

Sam Phippen Proxy manages all mocks and stubs for an object

Slide 127

Slide 127 text

Sam Phippen Proxy#add_stub creates a MethodDouble for the stubbed method

Slide 128

Slide 128 text

Sam Phippen AllowanceTarget TargetBase Receive Proxy Method double

Slide 129

Slide 129 text

Sam Phippen MethodDouble saves the original implementation

Slide 130

Slide 130 text

Sam Phippen MethodDouble creates the stub implementation

Slide 131

Slide 131 text

Sam Phippen RSpec.describe "mocking" do it “adds a foo method" do allow(foo).to receive(:bees) expect(foo.bees).to eq(nil) end end

Slide 132

Slide 132 text

Sam Phippen Calling foo.bees calls the stubbed implementation

Slide 133

Slide 133 text

Sam Phippen Call foo.bees

Slide 134

Slide 134 text

Sam Phippen Method double Call foo.bees

Slide 135

Slide 135 text

Sam Phippen Proxy Method double Call foo.bees

Slide 136

Slide 136 text

Sam Phippen Invoke stub Proxy Method double Call foo.bees

Slide 137

Slide 137 text

Sam Phippen Invoke stub Return value Proxy Method double Call foo.bees

Slide 138

Slide 138 text

Sam Phippen RSpec.describe "mocking" do it “adds a foo method" do allow(foo).to receive(:bees) expect(foo.bees).to eq(nil) end end

Slide 139

Slide 139 text

Sam Phippen At the end of the test, the MethodDouble puts the original method back on the object

Slide 140

Slide 140 text

Sam Phippen And that’s stubs

Slide 141

Slide 141 text

Sam Phippen RSpec.describe "mocking" do it “adds a foo method" do expect(foo).to receive(:bees) expect(foo.bees).to eq(nil) end end

Slide 142

Slide 142 text

Sam Phippen ExpectationTarget TargetBase Receive Proxy Method double

Slide 143

Slide 143 text

Sam Phippen At the end of each test, RSpec invokes RSpec::Mocks.verify

Slide 144

Slide 144 text

Sam Phippen RSpec::Mocks.verify enumerates all MethodDoubles

Slide 145

Slide 145 text

Sam Phippen MethodDoubles raise if their conditions weren’t met

Slide 146

Slide 146 text

Sam Phippen RSpec mocks has a lot more power, this is a high level overview

Slide 147

Slide 147 text

Sam Phippen rspec-core describe, it, runner, let, etc

Slide 148

Slide 148 text

Sam Phippen rspec-expectations expect keyword and matchers

Slide 149

Slide 149 text

Sam Phippen rspec-mocks allow, double, to receive, etc

Slide 150

Slide 150 text

Sam Phippen Thanks @samphippen [email protected]