class StatsD::Instrument::Matchers::Matcher
Public Class Methods
new(metric_type, metric_name, options = {})
click to toggle source
# File lib/statsd/instrument/matchers.rb, line 13 def initialize(metric_type, metric_name, options = {}) @metric_type = metric_type @metric_name = metric_name @options = options end
Public Instance Methods
description()
click to toggle source
# File lib/statsd/instrument/matchers.rb, line 38 def description "trigger a statsd call for metric #{@metric_name}" end
failure_message()
click to toggle source
# File lib/statsd/instrument/matchers.rb, line 26 def failure_message @message end
failure_message_when_negated()
click to toggle source
# File lib/statsd/instrument/matchers.rb, line 30 def failure_message_when_negated "No StatsD calls for metric #{@metric_name} expected." end
matches?(block)
click to toggle source
# File lib/statsd/instrument/matchers.rb, line 19 def matches?(block) expect_statsd_call(@metric_type, @metric_name, @options, &block) rescue RSpec::Expectations::ExpectationNotMetError => e @message = e.message false end
supports_block_expectations?()
click to toggle source
# File lib/statsd/instrument/matchers.rb, line 34 def supports_block_expectations? true end
Private Instance Methods
expect_statsd_call(metric_type, metric_name, options, &block)
click to toggle source
# File lib/statsd/instrument/matchers.rb, line 44 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 } if metrics.empty? raise RSpec::Expectations::ExpectationNotMetError, "No StatsD calls for metric #{metric_name} were made." elsif options[:times] && options[:times] != metrics.length raise RSpec::Expectations::ExpectationNotMetError, "The numbers of StatsD calls for metric " \ "#{metric_name} was unexpected. Expected #{options[:times].inspect}, got #{metrics.length}" end [: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 unless found message = metric_information(metric_name, options, metrics, expectation) raise RSpec::Expectations::ExpectationNotMetError, message end end true end
metric_information(metric_name, options, metrics, expectation)
click to toggle source
# 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