dry-rb/dry-rails

Application container not to be finalized in test env

stefanrendevski opened this issue · 0 comments

Components get booted immediately, before test setup is allowed to run. This means that items registered in the container from these boot files cannot be stubbed, before they are injected into other components that require them.

For example, the following setup will not work:

# config/system/boot/event.rb
MyApp::Container.boot(:event) do
  start do
     register('event_bus', MyEventBus.new)
  end
end

# config/system/boot/component.rb
MyApp::Container.boot(:component) do |container|
  use :event
  
  start do
    container['event_bus'].subscribe(MyListener.new, to: [MyEvent])
  end
end

# spec/rails_helper.rb
before do
  # Both :event and :component are already started at this point,
  # so this setup has no effect at all if container memoizes the registered items

  MyApp::Container.start(:event)
  MyApp::Container.stub('event_bus', TestDoubleEventBus.new)
  MyApp::Container.start(:component)
end

This behavior was already present in Dry::System::Rails, where the components had to be booted manually.

Resources

https://discourse.dry-rb.org/t/different-behavior-in-finalizing-container-between-dry-rails-and-dry-rails/1129