diff options
author | Bob Van Landuyt <bob@vanlanduyt.co> | 2017-12-04 18:58:44 +0300 |
---|---|---|
committer | Bob Van Landuyt <bob@vanlanduyt.co> | 2017-12-04 19:43:48 +0300 |
commit | 3d4ba90c5007cca3e8619afe8f40675962a9bc73 (patch) | |
tree | c7964fda4b6b8b31fd0bacfa0b0fd2649328e40a /spec/support | |
parent | 20f78421c82d626a3b43924146b7878fe4777ae8 (diff) |
Count occurrences of a specific query in the query recorder.
Diffstat (limited to 'spec/support')
-rw-r--r-- | spec/support/query_recorder.rb | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/spec/support/query_recorder.rb b/spec/support/query_recorder.rb index 369775db462..8cf8f45a8b2 100644 --- a/spec/support/query_recorder.rb +++ b/spec/support/query_recorder.rb @@ -41,7 +41,8 @@ RSpec::Matchers.define :exceed_query_limit do |expected| supports_block_expectations match do |block| - query_count(&block) > expected_count + threshold + @subject_block = block + actual_count > expected_count + threshold end failure_message_when_negated do |actual| @@ -55,6 +56,11 @@ RSpec::Matchers.define :exceed_query_limit do |expected| self end + def for_query(query) + @query = query + self + end + def threshold @threshold.to_i end @@ -68,12 +74,15 @@ RSpec::Matchers.define :exceed_query_limit do |expected| end def actual_count - @recorder.count + @actual_count ||= if @query + recorder.log.select { |recorded| recorded =~ @query }.size + else + recorder.count + end end - def query_count(&block) - @recorder = ActiveRecord::QueryRecorder.new(&block) - @recorder.count + def recorder + @recorder ||= ActiveRecord::QueryRecorder.new(&@subject_block) end def count_queries(queries) |