OK it’s a bit wordy but does the job for me for now… it would be nice if native dry.rb stubbing worked though. I imagine that if I were testing something more complicated, this approach would not work so well
This is because dry-rails is providing a Dry::System::Container, not a Dry::Core::Container directly. A Dry::System container is a wrapper that provides additional functionality on top of Dry::Core::Container.
Here’s how I setup container testing:
module RSpec::Testing
module StubHelpers
def container_stub(key, &block)
around :example do |example|
MyApp::Container.stub(key, instance_exec(&block)) { example.run }
end
end
end
end
require "dry/system/stubs"
MyApp::Container.enable_stubs!
RSpec.configure do |config|
config.extend RSpec::Testing::StubHelpers
end
And within a spec:
RSpec.describe "My Thing" do
let(:something) { Thing.new }
container_stub("my.thing") { something }
end
I also want to clarify that I don’t use stubs in all situations. If your thing under test uses autoinject dependencies, you can just pass them in as arguments: