Class: Statecraft::RSpec::RefuseEvent
- Inherits:
-
Object
- Object
- Statecraft::RSpec::RefuseEvent
- Defined in:
- lib/statecraft/rspec/refuse_event.rb
Overview
expect(record).to refuse_event(:cancel).because_of(:customer_cancellable?)
The named negation of allow_event: the refusal itself, and — through because_of — WHO refused. Guard names come from refusals_for, so because_of sees record-layer guards only; an input-reading guard: has no name there, and the failure message says so instead of pretending otherwise.
Instance Method Summary collapse
- #because_of(*guard_names) ⇒ Object
- #description ⇒ Object
- #failure_message ⇒ Object
- #failure_message_when_negated ⇒ Object
-
#initialize(event_name) ⇒ RefuseEvent
constructor
A new instance of RefuseEvent.
- #matches?(record) ⇒ Boolean
- #with_metadata(metadata) ⇒ Object
Constructor Details
#initialize(event_name) ⇒ RefuseEvent
Returns a new instance of RefuseEvent.
13 14 15 16 17 |
# File 'lib/statecraft/rspec/refuse_event.rb', line 13 def initialize(event_name) @event_name = event_name.to_sym @expected_guards = [] @metadata = {} end |
Instance Method Details
#because_of(*guard_names) ⇒ Object
19 20 21 22 |
# File 'lib/statecraft/rspec/refuse_event.rb', line 19 def because_of(*guard_names) @expected_guards = guard_names.map(&:to_sym) self end |
#description ⇒ Object
53 54 55 |
# File 'lib/statecraft/rspec/refuse_event.rb', line 53 def description "refuse event #{@event_name.inspect}" end |
#failure_message ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/statecraft/rspec/refuse_event.rb', line 38 def if @allowed return "expected #{StateReport.standing(@record)} to refuse event #{@event_name.inspect}, " \ "but the guards passed with metadata #{@metadata.inspect}" end missing = (@expected_guards - @refusing_guards).map(&:inspect).join(", ") "expected the refusal of #{@event_name.inspect} to come from #{missing}\n #{actual_refusers}" end |
#failure_message_when_negated ⇒ Object
48 49 50 51 |
# File 'lib/statecraft/rspec/refuse_event.rb', line 48 def "expected #{StateReport.standing(@record)} to allow event #{@event_name.inspect}, but it was refused\n " + StateReport.refusal(@record, @event_name, @metadata) end |
#matches?(record) ⇒ Boolean
29 30 31 32 33 34 35 36 |
# File 'lib/statecraft/rspec/refuse_event.rb', line 29 def matches?(record) @record = record @allowed = record.can_fire?(@event_name, metadata: @metadata) return false if @allowed @refusing_guards = record.refusals_for(@event_name).map(&:guard) (@expected_guards - @refusing_guards).empty? end |
#with_metadata(metadata) ⇒ Object
24 25 26 27 |
# File 'lib/statecraft/rspec/refuse_event.rb', line 24 def () @metadata = self end |