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(args) then_events InvoicePaidEvent(args)
Example for Rspec config
RSpec.configure do |config|
config.include Sequent::Test::CommandHandlerHelpers
end
Then in a spec
describe InvoiceCommandHandler do
before :each do
@event_store = Sequent::Test::CommandHandlerHelpers::FakeEventStore.new
@repository = Sequent::Core::AggregateRepository.new(@event_store)
@command_handler = InvoiceCommandHandler.new(@repository)
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
- #then_events(*events) ⇒ Object
- #then_no_events ⇒ Object
- #when_command(command) ⇒ Object
Instance Method Details
#given_events(*events) ⇒ Object
116 117 118 |
# File 'lib/sequent/test/command_handler_helpers.rb', line 116 def given_events *events @event_store.given_events(events.flatten(1)) end |
#then_events(*events) ⇒ Object
128 129 130 131 132 133 |
# File 'lib/sequent/test/command_handler_helpers.rb', line 128 def then_events *events expect(@event_store.stored_events.map(&:class)).to eq(events.flatten(1).map(&:class)) @event_store.stored_events.zip(events.flatten(1)).each do |actual, expected| expect(Sequent::Core::Oj.strict_load(Sequent::Core::Oj.dump(actual.payload))).to eq(Sequent::Core::Oj.strict_load(Sequent::Core::Oj.dump(expected.payload))) if expected end end |
#then_no_events ⇒ Object
135 136 137 |
# File 'lib/sequent/test/command_handler_helpers.rb', line 135 def then_no_events then_events end |
#when_command(command) ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/sequent/test/command_handler_helpers.rb', line 120 def when_command command raise "@command_handler is mandatory when using the #{self.class}" unless @command_handler raise "Command handler #{@command_handler} cannot handle command #{command}, please configure the command type (forgot an include in the command class?)" unless @command_handler.(command) @command_handler.(command) @repository.commit(command) @repository.clear end |