Module: Sequent::Test::CommandHandlerHelpers
- Defined in:
- lib/sequent/test/command_handler_helpers.rb
Overview
Use in tests
This provides a nice DSL for event based testing of your CommandHandler like
given_events InvoiceCreatedEvent.new(args) when_command PayInvoiceCommand.new(args) then_events InvoicePaidEvent.new(args)
Given events are applied against the Aggregate so need to represent a correct sequence of events.
When a command is executed all generated events are captured and can be retrieved using ‘stored_events` or tested using `then_events`.
The ‘then_events` expects one class, expected event, or RSpec matcher for each generated event, in the same order. Example for Rspec config. When a class is passed, only the type of the generated event is tested. When an expected event is passed only the payload is compared using the `have_same_payload_as` matcher defined by this module (`aggregate_id`, `sequence_number`, and `created_at` are not compared). When an RSpec matcher is passed the actual event is matched against this matcher, so you can use `eq` or `have_attributes` to do more specific matching.
RSpec.configure do |config|
config.include Sequent::Test::CommandHandlerHelpers
end
Then in a spec
describe InvoiceCommandHandler do
before :each do
Sequent.configuration.event_store = Sequent::Test::CommandHandlerHelpers::FakeEventStore.new
Sequent.configuration.command_handlers = [] # add your command handlers here
Sequent.configuration.event_handlers = [] # add you event handlers (eg, workflows) here
end
it "marks an invoice as paid" do
given_events InvoiceCreatedEvent.new(args)
when_command PayInvoiceCommand(args)
then_events InvoicePaidEvent(args)
end
end
Defined Under Namespace
Classes: FakeEventStore
Instance Method Summary collapse
- #given_events(*events) ⇒ Object
- #stored_events ⇒ Object
- #then_events(*expected_events) ⇒ Object
- #then_no_events ⇒ Object
- #when_command(command) ⇒ Object
Instance Method Details
#given_events(*events) ⇒ Object
152 153 154 155 156 157 |
# File 'lib/sequent/test/command_handler_helpers.rb', line 152 def given_events(*events) Sequent.configuration.event_store.commit_events( Sequent::Core::BaseCommand.new, to_event_streams(events.flatten(1)), ) end |
#stored_events ⇒ Object
180 181 182 |
# File 'lib/sequent/test/command_handler_helpers.rb', line 180 def stored_events Sequent.configuration.event_store.load_events_since_marked_position(@helpers_events_position_mark)[0] end |
#then_events(*expected_events) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/sequent/test/command_handler_helpers.rb', line 164 def then_events(*expected_events) matchers = expected_events.flatten(1).map do |expected| if expected.is_a?(Sequent::Core::Event) have_same_payload_as(expected) else expected end end expect(stored_events).to match(matchers) end |
#then_no_events ⇒ Object
176 177 178 |
# File 'lib/sequent/test/command_handler_helpers.rb', line 176 def then_no_events then_events end |
#when_command(command) ⇒ Object
159 160 161 162 |
# File 'lib/sequent/test/command_handler_helpers.rb', line 159 def when_command(command) @helpers_events_position_mark = Sequent.configuration.event_store.position_mark Sequent.configuration.command_service.execute_commands command end |