# File lib/statsd/instrument/matchers.rb, line 18 def initialize(metric_type, metric_name, options = {}) @metric_type = metric_type @metric_name = metric_name @options = options end
# File lib/statsd/instrument/matchers.rb, line 34 def failure_message @message end
# File lib/statsd/instrument/matchers.rb, line 38 def failure_message_when_negated "No StatsD calls for metric #{@metric_name} expected." end
# File lib/statsd/instrument/matchers.rb, line 24 def matches?(block) begin expect_statsd_call(@metric_type, @metric_name, @options, &block) rescue RSpec::Expectations::ExpectationNotMetError => e @message = e.message false end end
# File lib/statsd/instrument/matchers.rb, line 42 def supports_block_expectations? true end
# File lib/statsd/instrument/matchers.rb, line 48 def expect_statsd_call(metric_type, metric_name, options, &block) metrics = capture_statsd_calls(&block) metrics = metrics.select { |m| m.type == metric_type && m.name == metric_name } raise RSpec::Expectations::ExpectationNotMetError, "No StatsD calls for metric #{metric_name} were made." if metrics.empty? raise RSpec::Expectations::ExpectationNotMetError, "The numbers of StatsD calls for metric #{metric_name} was unexpected. Expected #{options[:times].inspect}, got #{metrics.length}" if options[:times] && options[:times] != metrics.length [:sample_rate, :value, :tags].each do |expectation| next unless options[expectation] num_matches = metrics.count do |m| matcher = RSpec::Matchers::BuiltIn::Match.new(options[expectation]) matcher.matches?(m.public_send(expectation)) end found = options[:times] ? num_matches == options[:times] : num_matches > 0 if !found message = metric_information(metric_name, options, metrics, expectation) raise RSpec::Expectations::ExpectationNotMetError, message end end true end
# File lib/statsd/instrument/matchers.rb, line 74 def metric_information(metric_name, options, metrics, expectation) message = "expected StatsD #{expectation.inspect} for metric '#{metric_name}' to be called" message += "\n " message += options[:times] ? "exactly #{options[:times]} times" : "at least once" message += " with: #{options[expectation]}" message += "\n captured metric values: #{metrics.map(&expectation).join(', ')}" message end