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)
Example for Rspec config
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
- #then_events(*expected_events) ⇒ Object
- #then_no_events ⇒ Object
- #when_command(command) ⇒ Object
Instance Method Details
#given_events(*events) ⇒ Object
143 144 145 |
# File 'lib/sequent/test/command_handler_helpers.rb', line 143 def given_events(*events) Sequent.configuration.event_store.given_events(events.flatten(1)) end |
#then_events(*expected_events) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/sequent/test/command_handler_helpers.rb', line 151 def then_events(*expected_events) expected_classes = expected_events.flatten(1).map { |event| event.instance_of?(Class) ? event : event.class } expect(Sequent.configuration.event_store.stored_events.map(&:class)).to eq(expected_classes) Sequent .configuration .event_store .stored_events .zip(expected_events.flatten(1)) .each_with_index do |(actual, expected), index| next if expected.instance_of?(Class) actual_hash = Sequent::Core::Oj.strict_load(Sequent::Core::Oj.dump(actual.payload)) expected_hash = Sequent::Core::Oj.strict_load(Sequent::Core::Oj.dump(expected.payload)) next unless expected # rubocop:disable Layout/LineLength expect(actual_hash) .to eq(expected_hash), "#{index + 1}th Event of type #{actual.class} not equal\nexpected: #{expected_hash.inspect}\n got: #{actual_hash.inspect}" # rubocop:enable Layout/LineLength end end |
#then_no_events ⇒ Object
175 176 177 |
# File 'lib/sequent/test/command_handler_helpers.rb', line 175 def then_no_events then_events end |
#when_command(command) ⇒ Object
147 148 149 |
# File 'lib/sequent/test/command_handler_helpers.rb', line 147 def when_command(command) Sequent.configuration.command_service.execute_commands command end |