Class: Statecraft::RSpec::AllowEvent

Inherits:
Object
  • Object
show all
Defined in:
lib/statecraft/rspec/allow_event.rb

Overview

expect(record).to allow_event(:pay).with_metadata("amount" => 5)

The can_fire? question, asked with the same metadata the production call will carry. The failure message walks the layers the pipeline walks: is the event declared from this state, and if so, which guards said no.

Instance Method Summary collapse

Constructor Details

#initialize(event_name) ⇒ AllowEvent

Returns a new instance of AllowEvent.



12
13
14
15
# File 'lib/statecraft/rspec/allow_event.rb', line 12

def initialize(event_name)
  @event_name = event_name.to_sym
  @metadata = {}
end

Instance Method Details

#descriptionObject



43
44
45
# File 'lib/statecraft/rspec/allow_event.rb', line 43

def description
  "allow event #{@event_name.inspect}"
end

#failure_messageObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/statecraft/rspec/allow_event.rb', line 27

def failure_message
  lines = ["expected #{StateReport.standing(@record)} to allow event #{@event_name.inspect}, but it was refused"]
  if StateReport.event_declared?(@record, @event_name)
    lines << StateReport.refusal(@record, @event_name, @metadata)
  else
    lines << "event #{@event_name.inspect} is not declared from #{StateReport.current_state(@record).inspect}"
    lines << StateReport.declared_shape_of(@record)
  end
  lines.join("\n  ")
end

#failure_message_when_negatedObject



38
39
40
41
# File 'lib/statecraft/rspec/allow_event.rb', line 38

def failure_message_when_negated
  "expected #{StateReport.standing(@record)} not to allow event #{@event_name.inspect}, " \
    "but the guards passed with metadata #{@metadata.inspect}"
end

#matches?(record) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
# File 'lib/statecraft/rspec/allow_event.rb', line 22

def matches?(record)
  @record = record
  record.can_fire?(@event_name, metadata: @metadata)
end

#with_metadata(metadata) ⇒ Object



17
18
19
20
# File 'lib/statecraft/rspec/allow_event.rb', line 17

def ()
  @metadata = 
  self
end