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



82
83
84
85
86
87
88
89
90
91
# File 'lib/sequent/test/event_handler_helpers.rb', line 82

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



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

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



57
58
59
60
61
62
63
64
65
# File 'lib/sequent/test/event_handler_helpers.rb', line 57

def then_events(*expected_events)
  expected_classes = expected_events.flatten(1).map { |event| event.class == 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.class == Class
    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_eventsObject



67
68
69
# File 'lib/sequent/test/event_handler_helpers.rb', line 67

def then_no_events
  then_events
end

#when_event(event) ⇒ Object



71
72
73
# File 'lib/sequent/test/event_handler_helpers.rb', line 71

def when_event(event)
  workflow.handle_message event
end