Contents

Method stubs

We're working on improving these docs. In the mean time, here's a cheat sheet to cover the basics.

# create a double
obj = double()

# stub a method
obj.stub(:message) # returns obj

# specify a return value
obj.stub(:message) { 'this is the value to return' }

Argument constraints

Explicit arguments
obj.stub(:message).with('an argument')
obj.stub(:message).with('more_than', 'one_argument')
Argument matchers
obj.stub(:message).with(anything())
obj.stub(:message).with(an_instance_of(Money))
obj.stub(:message).with(hash_including(:a => 'b'))
Regular expressions
obj.stub(:message).with(/abc/)

Raising/Throwing

obj.stub(:message) { raise "this error" }
obj.stub(:message) { throw :this_symbol }

Arbitrary handling

obj.stub(:message) do |arg1, arg2|
  # set expectations about the args in this block
  # and set a return value
end

Source: features/method_stubs/README.md at v2.4.0, RSpec Mocks (MIT license).