How to Stub Around a Call to the Original Method With Rspec ?

Update 05/23/2014: I created a gem for this, read an introduction here

Rspec mocks now features a ‘and_call_original’ method to create simple proxy mocks. But how could we build more complex proxies ?

For the sake of the subject, let’s take an example that is not a testing best practice, but that everybody will understand. Suppose you want to simulate a faulty network in your tests. You’d like to stub Net::HTTP.get so that it raises errors from time to time. Here is what you could do

1
2
3
4
5
6
7
i = 0
original_get = Net::HTTP.method(:get)
Net::HTTP.stub(:get) do |*args, &block|
  i = i+1
  raise RuntimeError.new("network down") if i%3 == 0
  original_get.call(*args, &block)
end

Note the block is taken into account, in this example, it does not matter so much, but forgetting it can bring up really strange issues.

Comments