method

assert_queries_count

assert_queries_count(count = nil, include_schema: false, &block)
public

Asserts that the number of SQL queries executed in the given block matches the expected count.

# Check for exact number of queries
assert_queries_count(1) { Post.first }

# Check for any number of queries
assert_queries_count { Post.first }

Any unmaterialized transactions will be materialized to ensure only queries attempted intside the block are counted.

If the :include_schema option is provided, any queries (including schema related) are counted. Setting this option also skips leasing a connection to materialize pending transactions since we want to count queries executed at connection open (e.g., type map).

assert_queries_count(1, include_schema: true) { Post.columns }