Module: Sequent::Test::EventStreamHelpers
- Defined in:
- lib/sequent/test/event_stream_helpers.rb
Overview
Use in tests
This provides a nice DSL for generating streams of events. FactoryBot is required when using this helper.
Example for Rspec config
RSpec.configure do |config|
config.include Sequent::Test::EventStreamHelpers
end
Then in a spec
given_stream_for(aggregate_id: ‘X’) do |s|
s.group_created owner_aggregate_id: 'Y'
s.group_opened
s.owner_joined_group owner_aggregate_id: 'Y'
end
Methods on ‘s` will be FactoryBot factories. All arguments will be passed on to FactoryBot’s build method. Aggregate ids and sequence numbers will be set automatically.
The example above can also be written as follows:
events = event_stream(aggregate_id: ‘X’) do |s|
s.group_created owner_aggregate_id: 'Y'
s.group_opened
s.owner_joined_group owner_aggregate_id: 'Y'
end
given_events(events)
Defined Under Namespace
Classes: Builder
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.included(_spec) ⇒ Object
75 76 77 78 79 |
# File 'lib/sequent/test/event_stream_helpers.rb', line 75 def self.included(_spec) require 'factory_bot' rescue LoadError raise ArgumentError, 'Factory bot is required to use the event stream helpers' end |
Instance Method Details
#event_stream(aggregate_id:, &block) ⇒ Object
65 66 67 68 69 |
# File 'lib/sequent/test/event_stream_helpers.rb', line 65 def event_stream(aggregate_id:, &block) builder = Builder.new(aggregate_id) block.call(builder) builder.events end |
#given_stream_for(aggregate_id:, &block) ⇒ Object
71 72 73 |
# File 'lib/sequent/test/event_stream_helpers.rb', line 71 def given_stream_for(aggregate_id:, &block) given_events(*event_stream(aggregate_id: aggregate_id, &block)) end |