Contents

stubbing with matchers

As a test author I want to be able to stub an invocation based on matchers' evaluations of the arguments so that I don't need to redundantly stub things I don't care about

Scenario Outline: stubbing

  • Given a new Dog test double
  • When I stub <method> to return <gives>
  • Then invoking <invocation> returns <returns>

Examples: the anything matcher with a one-argument method

methodgivesinvocationreturns
introduce_to(anything)'Why Hello!'introduce_to(Cat.new)'Why Hello!'
introduce_to(anything)'Why Hello!'introduce_to(Dog.new)'Why Hello!'
introduce_to(anything)'Why Hello!'introduce_to(nil)'Why Hello!'

Examples: the anything matcher with a two-argument method

methodgivesinvocationreturns
walk_to(anything,5)'Park'walk_to(5,5)'Park'
walk_to(anything,5)'Park'walk_to('pants',5)'Park'
walk_to(anything,5)'Park'walk_to(nil,5)'Park'
walk_to(anything,5)'Park'walk_to(3,5.1)nil
walk_to(3.123,anything)'Park'walk_to(3.123,nil)'Park'
walk_to(anything,anything)'Park'walk_to(3,5.1)'Park'

Examples: the anything matcher with a variable-argument method (argument size must match; but I don't know if I like some of these…)

methodgivesinvocationreturns
eat(anything,:fish,anything):yumeat(:cat,:fish,:mouse):yum
eat(anything,:fish,anything):yumeat(:cat,:pants,:mouse)nil
eat(anything):yumeat(:cat,:pants)nil
eat(:cat,anything):yumeat(:cat)nil
eat(:cat,anything):yumeat(:cat,nil):yum

Examples: the is_a matcher

methodgivesinvocationreturns
introduce_to(is_a(Animal)):howdyintroduce_to(Animal.new):howdy
introduce_to(is_a(Animal)):howdyintroduce_to(Cat.new):howdy
introduce_to(is_a(Animal)):howdyintroduce_to(Object.new)nil
introduce_to(is_a(Animal)):howdyintroduce_to(nil)nil

Examples: the any matcher (like is_a but also matches nil)

methodgivesinvocationreturns
introduce_to(any(Animal)):howdyintroduce_to(Animal.new):howdy
introduce_to(any(Animal)):howdyintroduce_to(Cat.new):howdy
introduce_to(any(Animal)):howdyintroduce_to(Object.new)nil
introduce_to(any(Animal)):howdyintroduce_to(nil):howdy

Examples: the numeric matcher

methodgivesinvocationreturns
walk_to(numeric,numeric):hydrantwalk_to(1.498,8):hydrant
walk_to(numeric,numeric):hydrantwalk_to(1.498,'string')nil
walk_to(numeric,numeric):hydrantwalk_to(1.498,nil)nil

Examples: the boolean matcher

methodgivesinvocationreturns
holler_at(boolean):ruffholler_at(true):ruff
holler_at(boolean):ruffholler_at(false):ruff
holler_at(boolean):ruffholler_at('woof')nil
holler_at(boolean):ruffholler_at(nil)nil

Source: features/old/stub_matchers.feature at commit f95ebe72, Gimme (MIT license).