RSpec matchers that check database index usage in ActiveRecord queries and code blocks.
- Ruby >= 2.7
- ActiveRecord >= 6.0 (supports SQLite and PostgreSQL)
Add this line to your application's Gemfile:
gem 'rspec-index-usage'And then execute:
$ bundle install
This will install the gem along with its runtime dependency on ActiveRecord >= 6.0.
Or install it yourself as:
$ gem install rspec-index-usage
Add to your Gemfile:
gem 'rspec-index-usage'Then in your specs:
require 'rspec-index-usage'
# In your specs
# Using a query
expect(User.where(email: 'test@example.com')).to use_index('index_users_on_email')
# Using a block
expect { User.where(email: 'test@example.com').first }.to have_used_index('index_users_on_email')
# Using a block but specifying one particular database connection
# This is only needed when you have more than one database setup
expect { User.where(email: 'test@example.com').first }.to have_used_index('index_users_on_email').on_connection(OtherDb.connection)Bug reports and pull requests are welcome on GitHub at https://github.com/PedroSena/rspec-index-usage.
The gem is available as open source under the terms of the MIT License.