Class: BrainzLab::Testing::Matchers::HaveBeenRecordedMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/brainzlab/testing/matchers.rb

Overview

Matcher for metrics

Instance Method Summary collapse

Constructor Details

#initializeHaveBeenRecordedMatcher

Returns a new instance of HaveBeenRecordedMatcher.



239
240
241
242
# File 'lib/brainzlab/testing/matchers.rb', line 239

def initialize
  @expected_value = nil
  @expected_tags = nil
end

Instance Method Details

#descriptionObject



285
286
287
288
289
290
# File 'lib/brainzlab/testing/matchers.rb', line 285

def description
  desc = "have recorded metric #{@type}('#{@name}')"
  desc += " with value #{@expected_value.inspect}" if @expected_value
  desc += " with tags #{@expected_tags.inspect}" if @expected_tags
  desc
end

#failure_messageObject



267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/brainzlab/testing/matchers.rb', line 267

def failure_message
  actual_metrics = BrainzLab::Testing.event_store.metrics_named(@name)

  if actual_metrics.empty?
    "expected metric #{@type}('#{@name}') to have been recorded, but no such metric was found.\n" \
      "Recorded metrics: #{BrainzLab::Testing.event_store.metrics.map { |m| "#{m[:type]}('#{m[:name]}')" }.inspect}"
  else
    msg = "expected metric #{@type}('#{@name}')"
    msg += " with value #{@expected_value.inspect}" if @expected_value
    msg += " with tags #{@expected_tags.inspect}" if @expected_tags
    msg + ", but recorded metrics were:\n  #{actual_metrics.inspect}"
  end
end

#failure_message_when_negatedObject



281
282
283
# File 'lib/brainzlab/testing/matchers.rb', line 281

def failure_message_when_negated
  "expected metric #{@type}('#{@name}') not to have been recorded, but it was"
end

#matches?(type_and_name) ⇒ Boolean

Returns:

  • (Boolean)


254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/brainzlab/testing/matchers.rb', line 254

def matches?(type_and_name)
  @type, @name = type_and_name
  @type = @type.to_sym
  @name = @name.to_s

  BrainzLab::Testing.event_store.metric_recorded?(
    @type,
    @name,
    value: @expected_value,
    tags: @expected_tags
  )
end

#with_tags(tags) ⇒ Object



249
250
251
252
# File 'lib/brainzlab/testing/matchers.rb', line 249

def with_tags(tags)
  @expected_tags = tags
  self
end

#with_value(value) ⇒ Object



244
245
246
247
# File 'lib/brainzlab/testing/matchers.rb', line 244

def with_value(value)
  @expected_value = value
  self
end