Module: Sequent::Test::WorkflowHelpers

Defined in:
lib/sequent/test/event_handler_helpers.rb

Overview

Use in tests

This provides a nice DSL for testing your event handlers. E.g.

when_event UserWasRegistered.new(args) then_commands SendWelcomeEmail.new(args)

Example for Rspec config

RSpec.configure do |config|

config.include Sequent::Test::WorkflowHelpers

end

Then in a spec

describe SendWelcomeMailWorkflow do

let(:workflow) { SendWelcomeMailWorkflow.new }

it "sends a welcome mail" do
  when_event UserWasRegistered.new(args)
  then_commands SendWelcomeEmail.new(args)
end

end

Defined Under Namespace

Classes: FakeCommandService, FakeTransactionProvider

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(spec) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/sequent/test/event_handler_helpers.rb', line 88

def self.included(spec)
  spec.let(:fake_command_service) { FakeCommandService.new }
  spec.let(:fake_transaction_provider) { FakeTransactionProvider.new }
  spec.before do
    Sequent.configure do |c|
      c.command_service = fake_command_service
      c.transaction_provider = fake_transaction_provider
    end
  end
end

Instance Method Details

#then_commands(*commands) ⇒ Object



81
82
83
84
85
86
# File 'lib/sequent/test/event_handler_helpers.rb', line 81

def then_commands(*commands)
  recorded = fake_command_service.recorded_commands
  expect(recorded.map(&:class)).to eq(commands.flatten(1).map(&:class))
  expect(fake_command_service.recorded_commands).to eq(commands.flatten(1))
  expect(recorded).to all(be_valid)
end

#then_events(*expected_events) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sequent/test/event_handler_helpers.rb', line 58

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 do |actual, expected|
    next if expected.instance_of?(Class)

    next unless 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)))
  end
end

#then_no_eventsObject



73
74
75
# File 'lib/sequent/test/event_handler_helpers.rb', line 73

def then_no_events
  then_events
end

#when_event(event) ⇒ Object



77
78
79
# File 'lib/sequent/test/event_handler_helpers.rb', line 77

def when_event(event)
  workflow.handle_message event
end