Search code examples
ruby-on-railsrspecvcr

How can I stub a dependency in a Rails service


I have:

  • a web service wrapper that calls a 3rd party API (lib)
  • a service that calls that wrapper and does some finagling (service)

Testing the lib class with VCR works great, but I am not sure how to test the service. Specifically, how can I stub the API call inside the service?

class GetBazsForBars
  def self.call(token)
    bazs = Foo.new(token).bars

    # do some other stuff
    bazs
  end
end

And the test:

require 'rails_helper'

RSpec.describe GetBazsForBars, type: :service do
  describe ".call" do
    let(:token) { Rails.configuration.x.test_token }
    let(:bazs)  { GetBazsForBars.call(token) }

    it { expect(bazs.any?).to eq(true) }
  end
end

I'd prefer not to pass in the Foo to the service.

In Payola the author uses a StripeMock to dummy up the responses. Can I write up something similar, like a FooMock that calls VCR and have the service use that instead?

Not sure how to do this with Ruby, I am used to C# / Java DI passing to constructors.

Thanks for the help.

Update

Moved to a full answer


Solution

  • foo = double(bars: 'whatever')
    allow(Foo).to receive(:new).with(:a_token).and_return foo
    

    or

    allow(Foo).to receive(:new).with(:a_token)
      .and_return double(bar: 'whatever')