You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
RSpec matchers for database queries made by ActiveRecord.
Installation
Add this line to your application's Gemfile, preferably in your test group:
gem'db-query-matchers'
And then execute:
bundle
Or install it yourself as:
gem install db-query-matchers
Usage
describe'MyCode'docontext'when we expect no queries'doit'does not make database queries'doexpect{subject.make_no_queries}.to_notmake_database_queriesendendcontext'when we expect queries'doit'makes database queries'doexpect{subject.make_some_queries}.tomake_database_queriesendendcontext'when we expect exactly 1 query'doit'makes database queries'doexpect{subject.make_one_query}.tomake_database_queries(count: 1)endendcontext'when we expect max 3 queries'doit'makes database queries'doexpect{subject.make_several_queries}.tomake_database_queries(count: 0..3)endendcontext'when we expect a possible range of queries'doit'makes database queries'doexpect{subject.make_several_queries}.tomake_database_queries(count: 3..5)endendcontext'when we only care about manipulative queries (INSERT, UPDATE, DELETE)'doit'makes a destructive database query'doexpect{subject.make_one_query}.tomake_database_queries(manipulative: true)endendcontext'when we only care about unscoped queries (SELECT without a WHERE or LIMIT clause))'doit'makes an unscoped database query'doexpect{subject.make_one_query}.tomake_database_queries(unscoped: true)endendcontext'when we only care about queries matching a certain pattern'doit'makes a destructive database query'doexpect{subject.make_special_queries}.tomake_database_queries(matching: 'DELETE * FROM')endit'makes a destructive database query matched with a regexp'doexpect{subject.make_special_queries}.tomake_database_queries(matching: /DELETE/)endendend
Configuration
To exclude certain types of queries from being counted, specify an
ignores configuration consisting of an array of regular expressions. If
a query matches one of the patterns in this array, it will not be
counted in the make_database_queries matcher.
To exclude queries previously cached by ActiveRecord from being counted,
add ignore_cached to the configuration.
To exclude SCHEMA queries, add schemaless to the configuration. This will
help avoid failing specs due to ActiveRecord load order.
To log more about the queries being made, you can set the log_backtrace
option to true. And to control what parts of the backtrace is logged,
you can use backtrace_filter.
DBQueryMatchers.configuredo |config|
config.ignores=[/SHOW TABLES LIKE/]config.ignore_cached=trueconfig.schemaless=true# the payload argument is described here:# https://edgeguides.rubyonrails.org/active_support_instrumentation.html#sql-active-recordconfig.on_query_counteddo |payload|
# do something arbitrary with the queryendconfig.log_backtrace=trueconfig.backtrace_filter=Proc.newdo |backtrace|
backtrace.select{ |line| line.start_with?(Rails.root.to_s)}endend