algoliasearch-rails icon indicating copy to clipboard operation
algoliasearch-rails copied to clipboard

`algolia/webmock` and `webmock/rspec` compatibility

Open nfabredev opened this issue 7 years ago • 3 comments

Hello everyone,

in my Rails 5.2 application, i'm indexing a model using the algoliasearch-rails gem.

In my test for a service class, which takes care of retrieving an object through an API and updating the model whenever there's changes to bring, i'm stubbing the requests to the Algolia API with:

# support/algolia.rb

require 'algolia/webmock'

RSpec.configure do |config|
  config.before(:each) do
    WebMock.enable!
  end

  config.after(:each) do
    WebMock.disable!
  end
end

However when i bring in 'webmock/rspec' and run the following test, calls are made to the API:

# spec/adapter_spec.rb

require 'rails_helper'
require 'algolia/webmock'

RSpec.describe 'Adapter::Object'
  context 'when raw object and model object are different' do
    let!(:object) { create(:object) }
    let(:adapter_object) { Adapter::object.new(raw_object) }

    it 'calls the Algolia API when adapting object' do
      expect(a_request(:any, /algolia/)).to have_been_made.at_least_once
      adapter_object.adapt
    end
  end
end

When i remove webmock/rspec no calls are made. It looks like it clears all stubs made by algolia/webmock.

Is there a way to configure these two to work together that i overlooked ?

nfabredev avatar Jun 08 '18 14:06 nfabredev

I'm seeing the same issue

tomjohn1028 avatar Sep 08 '18 18:09 tomjohn1028

I fixed this issue by calling the Algolia implementation explicitly:

require 'algolia/webmock'

Rspec.configuration do |config|
  config.before(:example) do
    Algolia::WebMock.mock!
  end
end

simonireilly avatar Nov 14 '18 07:11 simonireilly

I am getting error on this line (spec_helper.rb)

require "algolia/webmock"

Error backtrace:

Failure/Error: require "algolia/webmock"

LoadError:
cannot load such file -- algolia/webmock

Previously i was using 1.20.4 version, it was working fine. Now i have updated version to 2.2.2 then i am getting this error.

pragya-sriharsh avatar Jun 18 '23 12:06 pragya-sriharsh