Rails Rspec Testing with WebMock. Stubbing Class initialize if not production env?
<p>I'm new to testing and have run into a bit of a problem with testing external dependency responses.</p>

<p>My use case is that I have a gem Foo that that sends back some data based on passed in data. I use this gem when I initialize a class FooBar. The code for that looks similar to this:</p>

<pre><code>require 'foo'
class FooBar
  attr_accessor :thing

  def initialize(name)
    @thing = Foo(thing)
  end
end
</code></pre>

<p>I want to be able to stub the class initialization of FooBar in my rails test environment, but allow the external request to process in my production environment. In my test environment I'd like to just pass back an instance of FooBar with pre-supplied values. Right now I'm just trying to pass back a string.</p>

<p>I'm trying to use the WebMock gem for stubbing the initialization of FooBar right now with:</p>

<pre><code>require 'webmock/rspec'
  WebMock.disable_net_connect!(allow_localhost: true)

  RSpec.configure do |config|
    config.before(:each) do
      allow(foo_bar).to receive(:new).and_return('stubbed')
    end
  end
end
</code></pre>

<p>With that in my spec_helper file, and my tests checking the initialization of FooBar to equal 'stubbed'. The specific error it's throwing is:</p>

<pre><code>FooBar When I initialize class FooBar should be a string
FooBar
Failure/Error: Unable to find matching line from backtrace
NameError:
undefined local variable or method `foo_bar' for 
#&lt;RSpec::ExampleGroups::FooBar::WhenIInitializeClassFooBar&gt;
</code></pre>

<p>Is there something obvious that I'm missing?</p>