RSpec / RSpec Mocks / Docs / Stubs
This page comes from RSpec Mocks 2.1.0. It is not part of the 3.12.4 docs. See the latest RSpec Mocks docs.
Stub with substitute implementation
You can stub an implementation of a method (a.k.a. fake) by passing a block to the stub() method.
Scenario: stub implementation
- Given a file named "
stub_implementation_spec.rb" with:rubydescribe "a stubbed implementation" do it "works" do object = Object.new object.stub(:foo) do |arg| if arg == :this "got this" elsif arg == :that "got that" end end object.foo(:this).should eq("got this") object.foo(:that).should eq("got that") end end - When I run "
rspec ./stub_implementation_spec.rb" - Then the output should contain "
1 example, 0 failures"
Source: features/stubs/stub_implementation.feature at v2.1.0, RSpec Mocks (MIT license).