I'm new to testing and have run into a bit of a problem with testing external dependency responses.
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:
require 'foo'
class FooBar
attr_accessor :thing
def initialize(name)
@thing = Foo(thing)
end
end
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.
I'm trying to use the WebMock gem for stubbing the initialization of FooBar right now with:
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
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:
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
#<RSpec::ExampleGroups::FooBar::WhenIInitializeClassFooBar>
Is there something obvious that I'm missing?
Aucun commentaire:
Enregistrer un commentaire