Logo: Relish

  1. Sign up
  2. Sign in

Project: RSpec Mocks 2.14

stub with substitute implementation

You can stub an implementation of a method (a.k.a. fake) by passing a block
to the stub method.

Scenarios
stub implementation using `expect` syntax
Given
a file named "stub_implementation_spec.rb" with:
describe "a stubbed implementation" do
  it "works" do
    object = Object.new
    allow(object).to receive(:foo) do |arg|
      if arg == :this
        "got this"
      elsif arg == :that
        "got that"
      end
    end

    expect(object.foo(:this)).to eq("got this")
    expect(object.foo(:that)).to eq("got that")
  end
end
When
I run rspec stub_implementation_spec.rb
Then
the output should contain "1 example, 0 failures"
stub implementation using `should` syntax
Given
a file named "stub_implementation_spec.rb" with:
describe "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"

Last published over 2 years ago by myronmarston.