Slide 1

Slide 1 text

Follow along at: http://bit.ly/rspec_magic

Slide 2

Slide 2 text

Everybody loves RSpec

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

March, 2011

Slide 7

Slide 7 text

RSpec is complicated

Slide 8

Slide 8 text

While I think the benefits are discernable…

Slide 9

Slide 9 text

This is mostly about the complicated

Slide 10

Slide 10 text

Why is RSpec complicated? •RSpec is expressive •And very flexible •And it’s a big library

Slide 11

Slide 11 text

How flexible is RSpec?

Slide 12

Slide 12 text

With about 10 lines of configuration

Slide 13

Slide 13 text

This flexible… RSpec.! Hotel do
 " "works" do
 #(1 + 1).to ❤(2)
 end
 end

Slide 14

Slide 14 text

RSpec: It’s not actually Magic Noel Rappin Table XI

Slide 15

Slide 15 text

We’re going to look at RSpec source code

Slide 16

Slide 16 text

Please don’t avert your eyes

Slide 17

Slide 17 text

Why talk about RSpec internals?

Slide 18

Slide 18 text

Talks should tell a story

Slide 19

Slide 19 text

Once upon a time, there was a test… require 'rails_helper'
 
 RSpec.describe Name do
 it "produces a sort name" do
 name = Name.new("Noel", "Rappin")
 expect(name.sort_name).to eq("Rappin, Noel")
 end
 end

Slide 20

Slide 20 text

RSpec with Parentheses require 'rails_helper'
 
 RSpec.describe(Name) do
 self.it(“produces a sort name”) do
 name = Name.new("Noel", "Rappin")
 self.expect(name.sort_name).to(self.eq(“Rappin, Noel”))
 end
 end

Slide 21

Slide 21 text

Key RSpec words require 'rails_helper'
 
 RSpec.describe(Name) do
 self.it(“produces a sort name”) do
 name = Name.new("Noel", "Rappin")
 self.expect(name.sort_name).to(self.eq(“Rappin, Noel”))
 end
 end

Slide 22

Slide 22 text

RSpec power words describe it expect to(something)

Slide 23

Slide 23 text

RSpec power words describe it expect to(something) ExampleGroup Example ExpectationTarget Matcher

Slide 24

Slide 24 text

require 'rails_helper'
 
 RSpec.describe(Name) do
 self.it(“produces a sort name”) do
 name = Name.new("Noel", "Rappin")
 self.expect(name.sort_name).to(self.eq(“Rappin, Noel”))
 end
 end

Slide 25

Slide 25 text

require 'rails_helper'
 
 RSpec.describe(Name) do
 self.it(“produces a sort name”) do
 name = Name.new("Noel", "Rappin")
 self.expect(name.sort_name).to(self.eq(“Rappin, Noel”))
 end
 end Example Group

Slide 26

Slide 26 text

require 'rails_helper'
 
 RSpec.describe(Name) do
 self.it(“produces a sort name”) do
 name = Name.new("Noel", "Rappin")
 self.expect(name.sort_name).to(self.eq(“Rappin, Noel”))
 end
 end Example Group Example

Slide 27

Slide 27 text

require 'rails_helper'
 
 RSpec.describe(Name) do
 self.it(“produces a sort name”) do
 name = Name.new("Noel", "Rappin")
 self.expect(name.sort_name).to(self.eq(“Rappin, Noel”))
 end
 end Example Group Example ExpectationTarget

Slide 28

Slide 28 text

require 'rails_helper'
 
 RSpec.describe(Name) do
 self.it(“produces a sort name”) do
 name = Name.new("Noel", "Rappin")
 self.expect(name.sort_name).to(self.eq(“Rappin, Noel”))
 end
 end Example Group Example ExpectationTarget Matcher

Slide 29

Slide 29 text

Example Group • Created by describe or context • Arguments are a description, metadata, and block • Creates an anonymous subclass of ExampleGroup • Executes the block in the context of the class RSpec.describe Name, metadata: true do
 #stuff
 end

Slide 30

Slide 30 text

Creating an ExampleGroup def self.subclass(parent, description, args, &example_group_block)
 subclass = Class.new(parent)
 subclass.set_it_up(description, *args, &example_group_block)
 subclass.module_exec(&example_group_block) if example_group_block
 MemoizedHelpers.define_helpers_on(subclass)
 subclass
 end

Slide 31

Slide 31 text

Example Group Subclass def self.subclass(parent, description, args, &example_group_block)
 subclass = Class.new(parent)
 subclass.set_it_up(description, *args, &example_group_block)
 subclass.module_exec(&example_group_block) if example_group_block
 MemoizedHelpers.define_helpers_on(subclass)
 subclass
 end

Slide 32

Slide 32 text

Setup (configure mock package) def self.subclass(parent, description, args, &example_group_block)
 subclass = Class.new(parent)
 subclass.set_it_up(description, *args, &example_group_block)
 subclass.module_exec(&example_group_block) if example_group_block
 MemoizedHelpers.define_helpers_on(subclass)
 subclass
 end

Slide 33

Slide 33 text

Setup (configure mock package) def self.subclass(parent, description, args, &example_group_block)
 subclass = Class.new(parent)
 subclass.set_it_up(description, *args, &example_group_block)
 subclass.module_exec(&example_group_block) if example_group_block
 MemoizedHelpers.define_helpers_on(subclass)
 subclass
 end

Slide 34

Slide 34 text

Setup (configure mock package) def self.subclass(parent, description, args, &example_group_block)
 subclass = Class.new(parent)
 subclass.set_it_up(description, *args, &example_group_block)
 subclass.module_exec(&example_group_block) if example_group_block
 MemoizedHelpers.define_helpers_on(subclass)
 subclass
 end

Slide 35

Slide 35 text

What is module_exec? class Thing
 end
 
 Thing.module_exec(arg) do |arg|
 def hello() "Hello there!" end
 end 
 puts Thing.new.hello()

Slide 36

Slide 36 text

Minimal RSpec with Parentheses require 'rails_helper'
 
 <>.module_exec do
 it(“produces a sort name”) do
 name = Name.new("Noel", "Rappin")
 expect(name.sort_name).to(eq(“Rappin, Noel”))
 end
 end

Slide 37

Slide 37 text

Example • Created by it, example, specify — instance methods of ExampleGroup • Arguments are description, metadata, and a block • Assigns the example to an array class attribute of the ExampleGroup it(“produces a sort name”) do
 #stuff
 end

Slide 38

Slide 38 text

Creating an Example desc, *args = *all_args
 options = Metadata.build_hash_from(args) unless block
 options.update(:skip => RSpec::Core::Pending::NOT_YET_IMPLEMENTED) end options.update(extra_options)
 examples << RSpec::Core::Example.new(self, desc, options, block)
 examples.last

Slide 39

Slide 39 text

Creating an Example desc, *args = *all_args
 options = Metadata.build_hash_from(args) unless block
 options.update(:skip => RSpec::Core::Pending::NOT_YET_IMPLEMENTED) end options.update(extra_options)
 examples << RSpec::Core::Example.new(self, desc, options, block)
 examples.last

Slide 40

Slide 40 text

Creating an Example desc, *args = *all_args
 options = Metadata.build_hash_from(args) unless block
 options.update(:skip => RSpec::Core::Pending::NOT_YET_IMPLEMENTED) end options.update(extra_options)
 examples << RSpec::Core::Example.new(self, desc, options, block)
 examples.last

Slide 41

Slide 41 text

What happens at run time? •RSpec creates an instance of the anonymous ExampleGroup class •The new instance runs all of its examples

Slide 42

Slide 42 text

Running ExampleGroup runs examples ExampleGroup runs each example Example runs

Slide 43

Slide 43 text

Running an Example Group def self.run(reporter=RSpec::Core::NullReporter)
 return if RSpec.world.wants_to_quit
 reporter.example_group_started(self)
 should_run_context_hooks = descendant_filtered_examples.any?
 begin
 run_before_context_hooks(new('before(:context) hook')) if should_run_context_hooks
 result_for_this_group = run_examples(reporter)
 results_for_descendants = ordering_strategy.order(children).map { |child| child.run(reporter) }.all?
 result_for_this_group && results_for_descendants
 rescue Pending::SkipDeclaredInExample => ex
 for_filtered_examples(reporter) { |example| example.skip_with_exception(reporter, ex) }
 true
 rescue Exception => ex
 RSpec.world.wants_to_quit = true if fail_fast?
 for_filtered_examples(reporter) { |example| example.fail_with_exception(reporter, ex) }
 false
 ensure
 run_after_context_hooks(new('after(:context) hook')) if should_run_context_hooks
 reporter.example_group_finished(self)
 end
 end

Slide 44

Slide 44 text

Running an Example Group def self.run(reporter=RSpec::Core::NullReporter)
 return if RSpec.world.wants_to_quit
 reporter.example_group_started(self)
 should_run_context_hooks = descendant_filtered_examples.any?


Slide 45

Slide 45 text

Running an Example Group begin
 run_before_context_hooks(new('before(:context) hook')) if should_run_context_hooks
 result_for_this_group = run_examples(reporter)
 results_for_descendants = ordering_strategy.order(children).map { |child| child.run(reporter) }.all?
 result_for_this_group && results_for_descendants


Slide 46

Slide 46 text

Running an Example Group 
 rescue Pending::SkipDeclaredInExample => ex
 for_filtered_examples(reporter) { |example| example.skip_with_exception(reporter, ex) }
 true
 rescue Exception => ex
 RSpec.world.wants_to_quit = true if fail_fast?
 for_filtered_examples(reporter) { |example| example.fail_with_exception(reporter, ex) }
 false


Slide 47

Slide 47 text

Running an Example Group 
 ensure
 run_after_context_hooks(new('after(:context) hook')) if should_run_context_hooks
 reporter.example_group_finished(self)
 end
 end

Slide 48

Slide 48 text

Running an Example Group def self.run(reporter=RSpec::Core::NullReporter)
 return if RSpec.world.wants_to_quit
 reporter.example_group_started(self)
 should_run_context_hooks = descendant_filtered_examples.any?
 begin
 run_before_context_hooks(new('before(:context) hook')) if should_run_context_hooks
 result_for_this_group = run_examples(reporter)
 results_for_descendants = ordering_strategy.order(children).map { |child| child.run(reporter) }.all?
 result_for_this_group && results_for_descendants
 rescue Pending::SkipDeclaredInExample => ex
 for_filtered_examples(reporter) { |example| example.skip_with_exception(reporter, ex) }
 true
 rescue Exception => ex
 RSpec.world.wants_to_quit = true if fail_fast?
 for_filtered_examples(reporter) { |example| example.fail_with_exception(reporter, ex) }
 false
 ensure
 run_after_context_hooks(new('after(:context) hook')) if should_run_context_hooks
 reporter.example_group_finished(self)
 end
 end

Slide 49

Slide 49 text

Running Examples def self.run_examples(reporter)
 ordering_strategy.order(filtered_examples).map do |example|
 next if RSpec.world.wants_to_quit
 instance = new(example.inspect_output)
 set_ivars(instance, before_context_ivars)
 succeeded = example.run(instance, reporter)
 RSpec.world.wants_to_quit = true if fail_fast? && !succeeded
 succeeded
 end.all?
 end

Slide 50

Slide 50 text

Running Examples def self.run_examples(reporter)
 ordering_strategy.order(filtered_examples).map do |example|
 next if RSpec.world.wants_to_quit
 instance = new(example.inspect_output)
 set_ivars(instance, before_context_ivars)
 succeeded = example.run(instance, reporter)
 RSpec.world.wants_to_quit = true if fail_fast? && !succeeded
 succeeded
 end.all?
 end

Slide 51

Slide 51 text

Running Examples def self.run_examples(reporter)
 ordering_strategy.order(filtered_examples).map do |example|
 next if RSpec.world.wants_to_quit
 instance = new(example.inspect_output)
 set_ivars(instance, before_context_ivars)
 succeeded = example.run(instance, reporter)
 RSpec.world.wants_to_quit = true if fail_fast? && !succeeded
 succeeded
 end.all?
 end

Slide 52

Slide 52 text

Running Examples def self.run_examples(reporter)
 ordering_strategy.order(filtered_examples).map do |example|
 next if RSpec.world.wants_to_quit
 instance = new(example.inspect_output)
 set_ivars(instance, before_context_ivars)
 succeeded = example.run(instance, reporter)
 RSpec.world.wants_to_quit = true if fail_fast? && !succeeded
 succeeded
 end.all?
 end

Slide 53

Slide 53 text

Running Examples def self.run_examples(reporter)
 ordering_strategy.order(filtered_examples).map do |example|
 next if RSpec.world.wants_to_quit
 instance = new(example.inspect_output)
 set_ivars(instance, before_context_ivars)
 succeeded = example.run(instance, reporter)
 RSpec.world.wants_to_quit = true if fail_fast? && !succeeded
 succeeded
 end.all?
 end

Slide 54

Slide 54 text

Running Examples def self.run_examples(reporter)
 ordering_strategy.order(filtered_examples).map do |example|
 next if RSpec.world.wants_to_quit
 instance = new(example.inspect_output)
 set_ivars(instance, before_context_ivars)
 succeeded = example.run(instance, reporter)
 RSpec.world.wants_to_quit = true if fail_fast? && !succeeded
 succeeded
 end.all?
 end

Slide 55

Slide 55 text

Running Examples def self.run_examples(reporter)
 ordering_strategy.order(filtered_examples).map do |example|
 next if RSpec.world.wants_to_quit
 instance = new(example.inspect_output)
 set_ivars(instance, before_context_ivars)
 succeeded = example.run(instance, reporter)
 RSpec.world.wants_to_quit = true if fail_fast? && !succeeded
 succeeded
 end.all?
 end

Slide 56

Slide 56 text

Running One Example • Check that it is not pending • Run before blocks • instance_exec the block in the context of the example group • Raise is the example skips or fails • Run after blocks

Slide 57

Slide 57 text

begin
 run_before_example
 @example_group_instance.instance_exec(self, &@example_block)
 if pending?
 Pending.mark_fixed! self
 raise Pending::PendingExampleFixedError,
 'Expected example to fail since it is pending, but it passed.',
 [location]
 end
 rescue Pending::SkipDeclaredInExample
 # no-op, required metadata has already been set by the `skip`
 # method.
 rescue Exception => e
 set_exception(e)
 ensure
 run_after_example
 finish(reporter)
 end

Slide 58

Slide 58 text

begin
 run_before_example
 @example_group_instance.instance_exec(self, &@example_block)
 if pending?
 Pending.mark_fixed! self
 raise Pending::PendingExampleFixedError,
 'Expected example to fail since it is pending, but it passed.',
 [location]
 end
 rescue Pending::SkipDeclaredInExample
 # no-op, required metadata has already been set by the `skip`
 # method.
 rescue Exception => e
 set_exception(e)
 ensure
 run_after_example
 finish(reporter)
 end

Slide 59

Slide 59 text

begin
 run_before_example
 @example_group_instance.instance_exec(self, &@example_block)
 if pending?
 Pending.mark_fixed! self
 raise Pending::PendingExampleFixedError,
 'Expected example to fail since it is pending, but it passed.',
 [location]
 end
 rescue Pending::SkipDeclaredInExample
 # no-op, required metadata has already been set by the `skip`
 # method.
 rescue Exception => e
 set_exception(e)
 ensure
 run_after_example
 finish(reporter)
 end

Slide 60

Slide 60 text

begin
 run_before_example
 @example_group_instance.instance_exec(self, &@example_block)
 if pending?
 Pending.mark_fixed! self
 raise Pending::PendingExampleFixedError,
 'Expected example to fail since it is pending, but it passed.',
 [location]
 end
 rescue Pending::SkipDeclaredInExample
 # no-op, required metadata has already been set by the `skip`
 # method.
 rescue Exception => e
 set_exception(e)
 ensure
 run_after_example
 finish(reporter)
 end

Slide 61

Slide 61 text

begin
 run_before_example
 @example_group_instance.instance_exec(self, &@example_block)
 if pending?
 Pending.mark_fixed! self
 raise Pending::PendingExampleFixedError,
 'Expected example to fail since it is pending, but it passed.',
 [location]
 end
 rescue Pending::SkipDeclaredInExample
 # no-op, required metadata has already been set by the `skip`
 # method.
 rescue Exception => e
 set_exception(e)
 ensure
 run_after_example
 finish(reporter)
 end

Slide 62

Slide 62 text

begin
 run_before_example
 @example_group_instance.instance_exec(self, &@example_block)
 if pending?
 Pending.mark_fixed! self
 raise Pending::PendingExampleFixedError,
 'Expected example to fail since it is pending, but it passed.',
 [location]
 end
 rescue Pending::SkipDeclaredInExample
 # no-op, required metadata has already been set by the `skip`
 # method.
 rescue Exception => e
 set_exception(e)
 ensure
 run_after_example
 finish(reporter)
 end

Slide 63

Slide 63 text

Expectations and Matchers

Slide 64

Slide 64 text

expect(name.sort_name).to eq("Rappin, Noel”) ExpectationTarget.new(name.sortName)


Slide 65

Slide 65 text

An Expectation target responds to “to” and “not_to”

Slide 66

Slide 66 text

Which take a matcher as an argument and evaluate the matcher

Slide 67

Slide 67 text

A matcher responds to “matches?”

Slide 68

Slide 68 text

eq is a built in method of RSpec::Matchers def eq(expected)
 BuiltIn::Eq.new(expected)
 end

Slide 69

Slide 69 text

.to(Eq#"Rappin, Noel")

Slide 70

Slide 70 text

.to(Eq#"Rappin, Noel”) PositiveExpectationHandler.handle_matcher(“Rappin, Noel”, def match(expected, actual)
 actual == expected
 end

Slide 71

Slide 71 text

Let’s look at a fancier trick expect(Name.new("Noel Rappin")).to be_valid

Slide 72

Slide 72 text

BE_PREDICATE_REGEX = /^(be_(?:an?_)?)(.*)/
 HAS_REGEX = /^(?:have_)(.*)/
 
 def method_missing(method, *args, &block)
 case method.to_s
 when BE_PREDICATE_REGEX
 BuiltIn::BePredicate.new(method, *args, &block)
 when HAS_REGEX
 BuiltIn::Has.new(method, *args, &block)
 else
 super
 end
 end

Slide 73

Slide 73 text

class BePredicate < BaseMatcher
 include BeHelpers
 
 def initialize(*args, &block)
 @expected = parse_expected(args.shift)
 @args = args
 @block = block
 end
 
 def matches?(actual, &block)
 @actual = actual
 @block ||= block
 predicate_accessible? && predicate_matches?
 end
 
 def predicate_accessible?
 actual.respond_to?(predicate) || actual.respond_to?(present_tense_predicate)
 end
 
 def predicate_matches?
 method_name = actual.respond_to?(predicate) ? predicate : present_tense_predicate
 @predicate_matches = actual.__send__(method_name, *@args, &@block)
 end
 end

Slide 74

Slide 74 text


 
 def matches?(actual, &block)
 @actual = actual
 @block ||= block
 predicate_accessible? && predicate_matches?
 end
 
 def predicate_accessible?
 actual.respond_to?(predicate) || actual.respond_to?(present_tense_predicate)
 end
 
 def predicate_matches?
 method_name = actual.respond_to?(predicate) ? predicate : present_tense_predicate
 @predicate_matches = actual.__send__(method_name, *@args, &@block)
 end

Slide 75

Slide 75 text


 
 def matches?(actual, &block)
 @actual = actual
 @block ||= block
 predicate_accessible? && predicate_matches?
 end
 
 def predicate_accessible?
 actual.respond_to?(predicate) || actual.respond_to?(present_tense_predicate)
 end
 
 def predicate_matches?
 method_name = actual.respond_to?(predicate) ? predicate : present_tense_predicate
 @predicate_matches = actual.__send__(method_name, *@args, &@block)
 end

Slide 76

Slide 76 text


 
 def matches?(actual, &block)
 @actual = actual
 @block ||= block
 predicate_accessible? && predicate_matches?
 end
 
 def predicate_accessible?
 actual.respond_to?(predicate) || actual.respond_to?(present_tense_predicate)
 end
 
 def predicate_matches?
 method_name = actual.respond_to?(predicate) ? predicate : present_tense_predicate
 @predicate_matches = actual.__send__(method_name, *@args, &@block)
 end

Slide 77

Slide 77 text


 
 def matches?(actual, &block)
 @actual = actual
 @block ||= block
 predicate_accessible? && predicate_matches?
 end
 
 def predicate_accessible?
 actual.respond_to?(predicate) || actual.respond_to?(present_tense_predicate)
 end
 
 def predicate_matches?
 method_name = actual.respond_to?(predicate) ? predicate : present_tense_predicate
 @predicate_matches = actual.__send__(method_name, *@args, &@block)
 end

Slide 78

Slide 78 text

Emoji

Slide 79

Slide 79 text

define_example_group_method :example_group
 define_example_group_method :describe
 define_example_group_method :context
 define_example_group_method :xdescribe,
 :skip => "Temporarily skipped with xdescribe"
 define_example_group_method :xcontext,
 :skip => "Temporarily skipped with xcontext"
 define_example_group_method :fdescribe,
 :focus => true
 define_example_group_method :fcontext,
 :focus => true

Slide 80

Slide 80 text

define_example_method :specify
 define_example_method :focus, :focus => true
 define_example_method :fexample, :focus => true
 define_example_method :fit, :focus => true
 define_example_method :fspecify, :focus => true
 define_example_method :xexample, :skip => 'Temporarily skipped with xexample'
 define_example_method :xit, :skip => 'Temporarily skipped with xit'
 define_example_method :skip, :skip => true
 define_example_method :pending, :pending => true

Slide 81

Slide 81 text

require 'rails_helper'
 
 module RSpec
 module Core
 class ExampleGroup
 define_example_group_method :!
 define_example_method :" end
 end
 
 module Matchers
 def #(value=::RSpec::Expectations::ExpectationTarget::UndefinedValue, &block)
 ::RSpec::Expectations::ExpectationTarget.for(value, block)
 end
 alias_matcher :❤, :eq
 end
 end

Slide 82

Slide 82 text

And then… RSpec.! Hotel do
 " "works" do
 #(1 + 1).to ❤(2)
 end
 end

Slide 83

Slide 83 text

Mocks

Slide 84

Slide 84 text

Simple Mock Example require 'rails_helper'
 
 RSpec.describe Name do
 it "assigns a rating category" do
 user = User.new()
 expect(user).to receive(:credit_rating).and_return(1000)
 expect(user.rating_category).to eq("approved")
 end
 end

Slide 85

Slide 85 text

What would this mock need to do? •Ensure the method is not called •Track how often it’s called •Verify that expectations are met

Slide 86

Slide 86 text

Ruby Method Lookup path • Singleton class • instance methods of class • Included modules • parent of class • parent’s included modules • and so on…

Slide 87

Slide 87 text

Singleton Class class << x def foo #something end end def x.bar #something end x.foo; x.bar

Slide 88

Slide 88 text

Key Concepts • Receive is a matcher • Space • Proxy • Method Double Original Object Singleton Class MethodDouble Proxy Space Receive Matcher

Slide 89

Slide 89 text

Receive creates a matcher def receive(method_name, &block)
 Matchers::Receive.new(method_name, block)
 end

Slide 90

Slide 90 text

Inside the Receive Matcher def setup_expectation(subject, &block)
 warn_if_any_instance("expect", subject)
 @describable = setup_mock_proxy_method_substitute( subject, :add_message_expectation, block)
 end
 alias matches? setup_expectation
 
 def setup_mock_proxy_method_substitute(subject, method, block)
 proxy = ::RSpec::Mocks.space.proxy_for(subject)
 setup_method_substitute(proxy, method, block)
 end

Slide 91

Slide 91 text

Inside the Receive Matcher def setup_expectation(subject, &block)
 warn_if_any_instance("expect", subject)
 @describable = setup_mock_proxy_method_substitute( subject, :add_message_expectation, block)
 end
 alias matches? setup_expectation
 
 def setup_mock_proxy_method_substitute(subject, method, block)
 proxy = ::RSpec::Mocks.space.proxy_for(subject)
 setup_method_substitute(proxy, method, block)
 end

Slide 92

Slide 92 text

Inside the Receive Matcher def setup_expectation(subject, &block)
 warn_if_any_instance("expect", subject)
 @describable = setup_mock_proxy_method_substitute( subject, :add_message_expectation, block)
 end
 alias matches? setup_expectation
 
 def setup_mock_proxy_method_substitute(subject, method, block)
 proxy = ::RSpec::Mocks.space.proxy_for(subject)
 setup_method_substitute(proxy, method, block)
 end

Slide 93

Slide 93 text

Inside the Receive Matcher def setup_expectation(subject, &block)
 warn_if_any_instance("expect", subject)
 @describable = setup_mock_proxy_method_substitute( subject, :add_message_expectation, block)
 end
 alias matches? setup_expectation
 
 def setup_mock_proxy_method_substitute(subject, method, block)
 proxy = ::RSpec::Mocks.space.proxy_for(subject)
 setup_method_substitute(proxy, method, block)
 end

Slide 94

Slide 94 text

Proxy •Substitutes the doubled method •Tracks calls

Slide 95

Slide 95 text

Proxy adds Expectations def add_message_expectation(method_name, opts={}, &block)
 location = opts.fetch(:expected_from) { CallerFilter.first_non_rspec_line }
 meth_double = method_double_for(method_name)
 
 if null_object? && !block
 meth_double.add_default_stub(@error_generator, @order_group, location, opts) do
 @object
 end
 end
 
 meth_double.add_expectation @error_generator, @order_group, location, opts, &block
 end

Slide 96

Slide 96 text

Proxy adds Expectations def add_message_expectation(method_name, opts={}, &block)
 location = opts.fetch(:expected_from) { CallerFilter.first_non_rspec_line }
 meth_double = method_double_for(method_name)
 
 if null_object? && !block
 meth_double.add_default_stub(@error_generator, @order_group, location, opts) do
 @object
 end
 end
 
 meth_double.add_expectation @error_generator, @order_group, location, opts, &block
 end

Slide 97

Slide 97 text

Proxy adds Expectations def add_message_expectation(method_name, opts={}, &block)
 location = opts.fetch(:expected_from) { CallerFilter.first_non_rspec_line }
 meth_double = method_double_for(method_name)
 
 if null_object? && !block
 meth_double.add_default_stub(@error_generator, @order_group, location, opts) do
 @object
 end
 end
 
 meth_double.add_expectation @error_generator, @order_group, location, opts, &block
 end

Slide 98

Slide 98 text

Method Double def define_proxy_method
 return if @method_is_proxied
 save_original_method!
 definition_target.class_exec( self, method_name, visibility) do |method_double, method_name, visibility|
 define_method(method_name) do |*args, &block|
 method_double.proxy_method_invoked(self, *args, &block)
 end
 __send__(visibility, method_name)
 end
 @method_is_proxied = true
 end
 
 def proxy_method_invoked(_obj, *args, &block)
 @proxy.message_received method_name, *args, &block
 end

Slide 99

Slide 99 text

Method Double def define_proxy_method
 return if @method_is_proxied
 save_original_method!
 definition_target.class_exec( self, method_name, visibility) do |method_double, method_name, visibility|
 define_method(method_name) do |*args, &block|
 method_double.proxy_method_invoked(self, *args, &block)
 end
 __send__(visibility, method_name)
 end
 @method_is_proxied = true
 end
 
 def proxy_method_invoked(_obj, *args, &block)
 @proxy.message_received method_name, *args, &block
 end

Slide 100

Slide 100 text

Method Double def define_proxy_method
 return if @method_is_proxied
 save_original_method!
 definition_target.class_exec( self, method_name, visibility) do |method_double, method_name, visibility|
 define_method(method_name) do |*args, &block|
 method_double.proxy_method_invoked(self, *args, &block)
 end
 __send__(visibility, method_name)
 end
 @method_is_proxied = true
 end
 
 def proxy_method_invoked(_obj, *args, &block)
 @proxy.message_received method_name, *args, &block
 end

Slide 101

Slide 101 text

Method Double def define_proxy_method
 return if @method_is_proxied
 save_original_method!
 definition_target.class_exec( self, method_name, visibility) do |method_double, method_name, visibility|
 define_method(method_name) do |*args, &block|
 method_double.proxy_method_invoked(self, *args, &block)
 end
 __send__(visibility, method_name)
 end
 @method_is_proxied = true
 end
 
 def proxy_method_invoked(_obj, *args, &block)
 @proxy.message_received method_name, *args, &block
 end

Slide 102

Slide 102 text

Method Double def define_proxy_method
 return if @method_is_proxied
 save_original_method!
 definition_target.class_exec( self, method_name, visibility) do |method_double, method_name, visibility|
 define_method(method_name) do |*args, &block|
 method_double.proxy_method_invoked(self, *args, &block)
 end
 __send__(visibility, method_name)
 end
 @method_is_proxied = true
 end
 
 def proxy_method_invoked(_obj, *args, &block)
 @proxy.message_received method_name, *args, &block
 end

Slide 103

Slide 103 text

Expectations are verified after an example is torn down

Slide 104

Slide 104 text

See also http://rspec.info/documentation/

Slide 105

Slide 105 text

Noel Rappin Table XI @noelrap http://www.pragprog.com/book/nrtest2 (test2rappin for 25% off) http://www.noelrappin.com/trdd http://www.noelrappin.com/mstwjs