Skip to content

Commit

Permalink
Pass dependencies to ReservationProcess in the constructor
Browse files Browse the repository at this point in the history
Previous way was required when processes could be run from background job.
Passing dependencies in the constructor is usually preferrable and more testable.
  • Loading branch information
andrzejkrzywda committed Oct 1, 2024
1 parent 8cd551e commit 50624a8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
2 changes: 1 addition & 1 deletion ecommerce/processes/lib/processes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def enable_three_plus_one_free_process(event_store, command_bus)

def enable_reservation_process(event_store, command_bus)
event_store.subscribe(
ReservationProcess.new,
ReservationProcess.new(event_store, command_bus),
to: [
Ordering::OrderSubmitted,
Fulfillment::OrderCancelled,
Expand Down
6 changes: 3 additions & 3 deletions ecommerce/processes/lib/processes/reservation_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module Processes
class ReservationProcess
include Infra::Retry

def initialize
@event_store = Configuration.event_store
@command_bus = Configuration.command_bus
def initialize(event_store, command_bus)
@event_store = event_store
@command_bus = command_bus
end
attr_accessor :event_store, :command_bus

Expand Down
9 changes: 4 additions & 5 deletions ecommerce/processes/test/reservation_process_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ReservationProcessTest < Test
cover "Processes::ReservationProcess*"

def test_happy_path
process = ReservationProcess.new
process = ReservationProcess.new(event_store, command_bus)
assert_success_event do
given([order_submitted]).each { |event| process.call(event) }
end
Expand All @@ -31,8 +31,7 @@ def call(command)
def test_rejects_order_and_compensates_stock_when_sth_is_unavailable
failing_command = Inventory::Reserve.new(product_id: product_id, quantity: 1)
enhanced_command_bus = EnhancedFakeCommandBus.new(command_bus, failing_command => Inventory::InventoryEntry::InventoryNotAvailable)
process = ReservationProcess.new
process.command_bus = enhanced_command_bus
process = ReservationProcess.new(event_store, enhanced_command_bus)

assert_failure_event do
given([order_submitted]).each { |event| process.call(event) }
Expand All @@ -46,7 +45,7 @@ def test_rejects_order_and_compensates_stock_when_sth_is_unavailable
end

def test_release_stock_when_order_is_cancelled
process = ReservationProcess.new
process = ReservationProcess.new(event_store, command_bus)
given([order_submitted]).each { |event| process.call(event) }

command_bus.clear_all_received
Expand All @@ -58,7 +57,7 @@ def test_release_stock_when_order_is_cancelled
end

def test_dispatch_stock_when_order_is_confirmed
process = ReservationProcess.new
process = ReservationProcess.new(event_store, command_bus)
given([order_submitted]).each { |event| process.call(event) }

command_bus.clear_all_received
Expand Down

0 comments on commit 50624a8

Please sign in to comment.