Class: BrainzLab::Testing::EventExpectation

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

Overview

Fluent expectation builder for events

Instance Method Summary collapse

Constructor Details

#initialize(name, store) ⇒ EventExpectation

Returns a new instance of EventExpectation.



329
330
331
332
333
# File 'lib/brainzlab/testing/helpers.rb', line 329

def initialize(name, store)
  @name = name.to_s
  @store = store
  @expected_properties = {}
end

Instance Method Details

#failure_messageObject

Failure message for RSpec



368
369
370
371
372
373
374
375
# File 'lib/brainzlab/testing/helpers.rb', line 368

def failure_message
  if @expected_properties.empty?
    "expected event '#{@name}' to be tracked, but it wasn't"
  else
    "expected event '#{@name}' with properties #{@expected_properties.inspect} to be tracked, " \
      "but got: #{@store.events_named(@name).map { |e| e[:properties] }.inspect}"
  end
end

#failure_message_when_negatedObject

Negative failure message for RSpec



378
379
380
381
382
383
384
# File 'lib/brainzlab/testing/helpers.rb', line 378

def failure_message_when_negated
  if @expected_properties.empty?
    "expected event '#{@name}' not to be tracked, but it was"
  else
    "expected event '#{@name}' with properties #{@expected_properties.inspect} not to be tracked, but it was"
  end
end

#matching_eventsArray<Hash>

Get matching events

Returns:

  • (Array<Hash>)


360
361
362
363
364
365
# File 'lib/brainzlab/testing/helpers.rb', line 360

def matching_events
  events = @store.events_named(@name)
  return events if @expected_properties.empty?

  events.select { |e| properties_match?(e[:properties], @expected_properties) }
end

#satisfied?Boolean Also known as: matches?

Check if the expectation is satisfied

Returns:

  • (Boolean)


349
350
351
# File 'lib/brainzlab/testing/helpers.rb', line 349

def satisfied?
  @store.event_tracked?(@name, @expected_properties.empty? ? nil : @expected_properties)
end

#with(properties = {}) ⇒ self

Specify expected properties

Parameters:

  • properties (Hash) (defaults to: {})

    Properties to match

Returns:

  • (self)


340
341
342
343
# File 'lib/brainzlab/testing/helpers.rb', line 340

def with(properties = {})
  @expected_properties = properties
  self
end