Module: Sequent::Test::WorkflowHelpers

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

Overview

Use in tests

This provides a nice DSL for testing your Workflows. 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, workflows: true

end

Please note that you must add the metadata tag ‘workflows` since the WorkflowHelpers will use a `FakeCommandService` to track the commands enqueued for execution. You can set the metadata on group level or on an individual spec.

describe SendWelcomeMailWorkflow, workflows: true 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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/sequent/test/workflow_helpers.rb', line 93

def self.included(spec)
  fail "Missing metadata argument `workflows: true` when including #{name}" unless spec.[:workflows]

  spec.let(:fake_command_service) { FakeCommandService.new }
  spec.let(:fake_transaction_provider) { FakeTransactionProvider.new }
  spec.let(:old_config) { Sequent.configuration }
  spec.before :each, :workflows do
    new_config = old_config.dup

    new_config.command_service = fake_command_service
    new_config.transaction_provider = fake_transaction_provider

    Sequent::Configuration.restore(new_config)
  end

  spec.after :each, :workflows do
    Sequent::Configuration.restore(old_config)
  end
end

Instance Method Details

#then_commands(*commands) ⇒ Object



86
87
88
89
90
91
# File 'lib/sequent/test/workflow_helpers.rb', line 86

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sequent/test/workflow_helpers.rb', line 61

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



76
77
78
# File 'lib/sequent/test/workflow_helpers.rb', line 76

def then_no_events
  then_events
end

#when_event(event) ⇒ Object



80
81
82
83
84
# File 'lib/sequent/test/workflow_helpers.rb', line 80

def when_event(event)
  fake_transaction_provider.transactional do
    workflow.handle_message event
  end
end