Webmock configuration gone since 2.0.0
- 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.
I used the sample configuration provided here with algolia/webmock too. It's not working in 2.0.0.
Looks like there was a PR to add this back which was closed? https://github.com/algolia/algoliasearch-client-ruby/pull/420
Webmock has been replaced by a MockRequester
MockRequester is only on algoliasearch-client-ruby... This is algoliasearch-rails
I wanted to upgrade today but needed to maintain my testing infrastructure so came up with a hacky solution.
- 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: {}
)
- I put this at the top of my
test_helperfile.
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
- 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
MockRequester is only on algoliasearch-client-ruby... This is algoliasearch-rails
this gem requires algolia which is algoliasearch-client-ruby, so it's avaliable