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
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.