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
75 76 77 78 |
# File 'lib/sequent/test/command_handler_helpers.rb', line 75 def given_events *events raise ArgumentError.new("events can not be nil") if events.compact.empty? @event_store.given_events(events) end |
#then_events(*events) ⇒ Object
87 88 89 90 91 92 |
# File 'lib/sequent/test/command_handler_helpers.rb', line 87 def then_events *events @event_store.stored_events.map(&:class).should == events.map(&:class) @event_store.stored_events.zip(events).each do |actual, expected| JSON.parse(actual.payload.to_json).should == JSON.parse(expected.payload.to_json) if expected end end |
#then_no_events ⇒ Object
94 95 96 |
# File 'lib/sequent/test/command_handler_helpers.rb', line 94 def then_no_events then_events end |
#when_command(command) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/sequent/test/command_handler_helpers.rb', line 80 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) end |