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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(spec) ⇒ Object



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

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

Instance Method Details

#then_commands(*commands) ⇒ Object



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

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



42
43
44
45
46
47
48
49
50
# File 'lib/sequent/test/event_handler_helpers.rb', line 42

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



52
53
54
# File 'lib/sequent/test/event_handler_helpers.rb', line 52

def then_no_events
  then_events
end

#when_event(event) ⇒ Object



56
57
58
# File 'lib/sequent/test/event_handler_helpers.rb', line 56

def when_event(event)
  workflow.handle_message event
end