Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
assert_queries - rails 4
# Assertion for checking the number of queries executed within the &block
def assert_queries(num = 1, &block)
queries = []
callback = lambda { |name, start, finish, id, payload|
queries << payload[:sql] if payload[:sql] =~ /^SELECT|UPDATE|INSERT/
ActiveSupport::Notifications.subscribed(callback, "sql.active_record", &block)
assert_equal num, queries.size, "#{queries.size} instead of #{num} queries were executed.#{queries.size == 0 ? '' : "\nQueries:\n#{queries.join("\n")}"}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment