Skip to content

Instantly share code, notes, and snippets.

@amitpatelx
Created July 4, 2023 05:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amitpatelx/85ab8634822e93a5db42b954e561a7d0 to your computer and use it in GitHub Desktop.
Save amitpatelx/85ab8634822e93a5db42b954e561a7d0 to your computer and use it in GitHub Desktop.
Test N+1 in Spec
# Source: https://evilmartians.com/chronicles/squash-n-plus-one-queries-early-with-n-plus-one-control-test-matchers-for-ruby-and-rails
# Gemfile
gem 'n_plus_one_control', group: :test
# rails_helper.rb
require 'n_plus_one_control/rspec'
# spec.rb
context 'N+1', :n_plus_one do
populate { |n| create_list(:reservation_detail, n) }
specify do
expect { described_class.new(Date.current, Date.current.next_month).n1 }.to perform_constant_number_of_queries
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment