Developing and testing
Connection errors
Are you getting “Impossible to connect”, “Unable to connect”, or “Unreachable hosts” errors? First, make sure the issue isn’t at your end:
- Ensure you’re using the correct application ID and API key. Find these credentials on your Algolia dashboard.
- Check for recent changes in your code.
- Check the status of your data center provider.
If you’re using Firebase, you can only access Algolia from a paid Firebase tier.
If you can’t solve the problem yourself, contact the Algolia support team and provide them with the following information:
- The name of your framework integration (Rails) and its version number
- A code snippet to reproduce the issue
- Error message or stack trace (if applicable)
- The name of the Algolia index that’s causing problems
- The exact UTC time of the event
- If you can’t connect to the Algolia API from your browser, send the output from community.algolia.com/diag/.
-
If you can’t connect to the Algolia API from your servers, send the output from the following command (run on any affected server):
Copy1
curl -sL https://algolia.com/downloads/diag.sh > ./diag.sh && sudo ./diag.sh YourApplicationID
Replace
YourApplicationID
with your Algolia application ID.
Raise or log exceptions
By default, this gem raises exceptions if something goes wrong.
You can control this with the raise_on_failure
option.
If set to false, all exceptions are logged.
1
2
3
4
5
6
7
8
class Contact < ActiveRecord::Base
include AlgoliaSearch
# Only raise exceptions if not in a production environment
algoliasearch raise_on_failure: !Rails.env.production? do
attribute :first_name, :last_name, :email
end
end
Turn off indexing
Use the following code snippet to turn off indexing based on the current Rails environment. This can be helpful during testing or development.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Contact < ActiveRecord::Base
include AlgoliaSearch
algoliasearch disable_indexing: Rails.env.test? do
# Algolia configuration
end
end
class Contact < ActiveRecord::Base
include AlgoliaSearch
algoliasearch disable_indexing: Proc.new { Rails.env.test? || more_complex_condition } do
# Algolia configuration
end
end
Wait for indexing tasks
Indexing operations are asynchronous by default. During your tests, you might create models and then confirm if they were indexed in Algolia. To test this, it’s best to make your indexing synchronous.
1
2
3
4
5
6
7
class Contact < ActiveRecord::Base
include AlgoliaSearch
algoliasearch synchronous: Rails.env.test? do
# Algolia configuration
end
end
This option should only be used for testing purposes.
Mock the Algolia API
To mock your API calls to the Algolia API,
use the MockRequester
class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
describe 'With a mocked client' do
before(:all) do
mock_requester = MockRequester.new
mock_client = Algolia::Search::Client.new(
Algolia::Search::Config.new(AlgoliaSearch.configuration),
http_requester: mock_requester
)
# Override the client
AlgoliaSearch.instance_variable_set(:@client, mock_client)
end
it "shouldn't perform any API calls here" do
User.create(name: 'My Indexed User') # mocked, no API call performed
User.search('').should == {} # mocked, no API call performed
end
end
Run tests for the Algolia Search gem
If you want to contribute to this gem, you can run the tests locally or let the CI run them when opening your pull requests.
Before you run the tests, set the ALGOLIA_APPLICATION_ID
and ALGOLIA_API_KEY
environment variables.
Since the tests create and delete indices, make sure the index names won’t affect your production.