Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tag: v3.0.0.beta1

Fetching latest commit…

Cannot retrieve the latest commit at this time

..
Failed to load latest commit information.
README.md
any_instance.feature
as_null_object.feature
simple_return_value_with_allow.feature
simple_return_value_with_stub.feature
stub_chain.feature
stub_implementation.feature
to_ary.feature

README.md

Stub return values

# create a double
obj = double()

# specify a return value using `:expect` syntax
allow(obj).to receive(:message) { :value }
allow(obj).to receive(:message).and_return(:value)

# specify a return value using `:should` syntax
obj.stub(:message) { :value }
obj.stub(:message => :value)
obj.stub(:message).and_return(:value)

These forms are somewhat interchangeable. The difference is that the block contents are evaluated lazily when the obj receives the message message, whereas the others are evaluated as they are read.

Fake implementation

allow(obj).to receive(:message) do |arg1, arg2|
  # set expectations about the args in this block
  # and/or return  value
end

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

Raising/Throwing

allow(obj).to receive(:message).and_raise("this error")
allow(obj).to receive(:message).and_throw(:this_symbol)

obj.stub(:message).and_raise("this error")
obj.stub(:message).and_throw(:this_symbol)

You can also use the block format:

allow(obj).to receive(:message) { raise "this error" }
allow(obj).to receive(:message) { throw :this_symbol }

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

Argument constraints

Explicit arguments

allow(obj).to receive(:message).with('an argument') { ... }

obj.stub(:message).with('an argument') { ... }
obj.stub(:message).with('more_than', 'one_argument') { ... }

Argument matchers

allow(obj).to receive(:message).with(anything()) { ... }
allow(obj).to receive(:message).with(an_instance_of(Money)) { ... }
allow(obj).to receive(:message).with(hash_including(:a => 'b')) { ... }
allow(obj).to receive(:message).with(array_including(1,2,3)) { ... }
# or
allow(obj).to receive(:message).with(array_including([1,2,3])) { ... }

obj.stub(:message).with(anything()) { ... }
obj.stub(:message).with(an_instance_of(Money)) { ... }
obj.stub(:message).with(hash_including(:a => 'b')) { ... }
obj.stub(:message).with(array_including(1,2,3)) { ... }
# or
obj.stub(:message).with(array_including([1,2,3])) { ... }

Regular expressions

allow(obj).to receive(:message).with(/abc/) { ... }

obj.stub(:message).with(/abc/) { ... }
Something went wrong with that request. Please try again.