module ArQueryMatchers::ArQueryMatchers::MatcherErrors

Public Instance Methods

difference(keys) click to toggle source

Show the difference between expected and actual values with one value per line. This is done by hand because as of this writing the author doesn't understand how RSpec does its nice hash diff printing.

# File lib/ar_query_matchers.rb, line 209
def difference(keys)
  max_key_length = keys.reduce(0) { |max, key| [max, key.size].max }

  keys.map do |key|
    left = expected.fetch(key, 0)
    right = @query_stats.queries.fetch(key, {}).fetch(:count, 0)

    diff = "#{'+' if right > left}#{right - left}"

    "#{key.rjust(max_key_length, ' ')} – expected: #{left}, got: #{right} (#{diff})"
  end.compact
end
expectation_failed_message(crud_operation) click to toggle source
# File lib/ar_query_matchers.rb, line 241
def expectation_failed_message(crud_operation)
  all_model_names = expected.keys + @query_stats.queries.keys
  model_names_with_wrong_count = all_model_names.reject { |key| expected[key] == @query_stats.queries[key][:count] }.uniq
  "Expected ActiveRecord to #{crud_operation} #{expected}, got #{@query_stats.query_counts}\nExpectations that differed:\n#{difference(model_names_with_wrong_count).join("\n")}\n\nWhere unexpected queries came from:\n\n#{source_lines(model_names_with_wrong_count).join("\n")}"
end
no_queries_fail_message(crud_operation) click to toggle source
# File lib/ar_query_matchers.rb, line 237
def no_queries_fail_message(crud_operation)
  "Expected ActiveRecord to not #{crud_operation} any records, got #{@query_stats.query_counts}\n\nWhere unexpected queries came from:\n\n#{source_lines(@query_stats.query_counts.keys).join("\n")}"
end
source_lines(keys) click to toggle source
# File lib/ar_query_matchers.rb, line 222
def source_lines(keys)
  line_frequency = @query_stats.query_lines_by_frequency
  keys_with_source_lines = keys.select { |key| line_frequency[key].present? }
  keys_with_source_lines.map do |key|
    source_lines = line_frequency[key].sort_by(&:last).reverse # Most frequent on top
    next if source_lines.blank?

    [
      "  #{key}"
    ] + source_lines.map { |line, count| "    #{count} #{'call'.pluralize(count)}: #{line}" } + [
      ''
    ]
  end
end