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

Webmock configuration gone since 2.0.0

Open strayer opened this issue 4 years ago • 6 comments

  • Rails version: 6.0.3.6
  • Algolia Rails integration version: 2.0.0
  • Algolia Client Version: 2.0.4
  • Language Version: 2.7.3p183

Description

The README mentions the algolia webmock sample configuration in algolia/webmock. This is used by us heavily in the tests, but seems to be gone without a trace in the newest algolia client version.

strayer avatar May 26 '21 15:05 strayer

I used the sample configuration provided here with algolia/webmock too. It's not working in 2.0.0.

dirceu-jr avatar May 31 '21 18:05 dirceu-jr

Looks like there was a PR to add this back which was closed? https://github.com/algolia/algoliasearch-client-ruby/pull/420

magni- avatar Jun 17 '21 02:06 magni-

Webmock has been replaced by a MockRequester

bvogel avatar Jun 28 '21 13:06 bvogel

I wanted to upgrade today but needed to maintain my testing infrastructure so came up with a hacky solution.

  1. Copied the MockRequester in the comment ^ and slightly modified it
    # Need to add "status: published" otherwise we get stuck in an infinite loop due to algolia/search_index "wait_task"
    Algolia::Http::Response.new(
      status: 204,
      body: '{"hits": [], "status": "published"}',
      headers: {}
    )
  1. I put this at the top of my test_helper file.
MOCK_REQUESTOR = nil
if Rails.env.test?
  MOCK_REQUESTOR = MockRequester.new  # Copied the mock requestor for 
  mock_client = Algolia::Search::Client.new(
    Algolia::Search::Config.new(AlgoliaSearch.configuration),
    http_requester: MOCK_REQUESTOR
  )
  # AlgoliaSearch isn't set up to override the clicent but we can do it anyways
  AlgoliaSearch.instance_variable_set(:@client, mock_client)
end
  1. I then have some helper functions that I make available to all my tests. This is more related to my testing setup but figured it might be helpful.
module ActiveSupport
  class TestCase
    def initialize(*args)
      super
      @mock_requester = MOCK_REQUESTOR
    end
  
    def reset_algolia_requests
      @mock_requester.requests = []
    end


    def algolia_requests(model = nil)
      requests = @mock_requester.requests
      # Ignore get requests, don't care about those in context of out testing (just looking for indexing calls)
      requests = requests.select { |r| %i[post put].include? r[:method] }
      return requests.select { |r| r[:path].include? model.index_name } if model
      requests
    end

    # The include objects argument is really just here so that we can "pretty print" the output without a huge blob more easily
    def algolia_reindex_request_summary(model = nil, include_objects: false)
      algolia_requests(model).map do |r|
        body = ::JSON.parse(r[:body])['requests']
        {
          path: r[:path][3..], # Get rid of the "/1/" atthe front
          index_name: r[:path][3..].split('/')[1], # Isolates only the index name
          ids: (body.map { |b| b['objectID'] }).sort,
          objects: body.map { |b| b['body'] }
          body_count: body.count,
        }
      end
    end
  end

allanohorn avatar Nov 25 '21 01:11 allanohorn

MockRequester is only on algoliasearch-client-ruby... This is algoliasearch-rails

this gem requires algolia which is algoliasearch-client-ruby, so it's avaliable

bvogel avatar Nov 25 '21 10:11 bvogel