21

How can I stub a method only on the first call, and in the second one it should behave as expected?

I have the following method:

def method
  do_stuff
rescue => MyException
  sleep rand
  retry
end

I want to the first call of do_stuff to raise MyException, but in the second call, behaves normally. I need to achieve this to test my rescue block without getting an infinite loop.

Is there a way to achieve this?

fotanus
  • 19,618
  • 13
  • 77
  • 111

1 Answers1

19

You can pass a block to a stub that will be invoked when the stub is called. You can then perform the unstub in there, in addition to doing whatever you need to.

class Foo
  def initialize
    @calls = 0
  end

  def be_persistent
    begin
      increment
    rescue
      retry
    end
  end

  def increment
    @calls += 1
  end
end

describe "Stub once" do
  let(:f) { Foo.new }
  before {
    f.stub(:increment) { f.unstub(:increment); raise "boom" }
  }

  it "should only stub once" do
    f.be_persistent.should == 1
  end
end

Seems to work nicely here.

$ rspec stub.rb -f doc

Stub once
  should only stub once

Finished in 0.00058 seconds
1 example, 0 failures

Alternately, you could just track the number of calls and return different results for the stub based on the call count:

describe "Stub once" do
  let(:f) { Foo.new }

  it "should return different things when re-called" do
    call_count = 0
    f.should_receive(:increment).twice {
      if (call_count += 1) == 1
        raise "boom"
      else
        "success!"
      end
    }

    f.be_persistent.should == "success!"
  end
end
Chris Heald
  • 61,439
  • 10
  • 123
  • 137
  • This solved a problem I ran into with ActiveRecord's readonly feature--I had a model marked readonly (`def readonly; true; end`) but needed to create the instances in a test and didn't want to mux test code with production code. The solution (in a factory_girl factory): `after(:build) do |f| f.stub(:readonly?) { f.unstub(:readonly?); false }; f.save!; end` – Isaac Betesh May 15 '14 at 19:44