Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Allows for assertions regarding number of queries executed.
- #
- # Usage:
- # it "eager loads `#manager` association" do
- # expect do
- # employee = Employee.with_manager.find(employee_id)
- # employee.manager
- # end.to satisfy_query_limit(1)
- # end
- RSpec::Matchers.define :satisfy_query_limit do |expected|
- match do |block|
- count_queries(&block) <= expected
- end
- failure_message_for_should do |actual|
- "Expected to execute no more than #{expected} SQL queries, got #{@query_count}"
- end
- failure_message_for_should_not do |actual|
- "Expected to execute more than #{expected} SQL queries, got #{@query_count}"
- end
- def count_queries
- @query_count = 0
- filter_list = %w[CACHE SCHEMA]
- # Rails 3.2 introduces ActiveSupport::Notifications::subscribed for temporary
- # subscribers. Use that for 3.2+
- query_counter = ->(name, started, finished, unique_id, payload) do
- @query_count += 1 unless payload[:name].in?(filter_list)
- end
- subscriber = ActiveSupport::Notifications.subscribe("sql.active_record", query_counter)
- yield
- ActiveSupport::Notifications.unsubscribe(subscriber)
- @query_count
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement