Inject a wrapped operation in dry-transaction?


#1

Hi,

I have a transaction with an operation that is wrapped.

class Import
  include Dry::Transaction(container: MyContainer)

  step :store_product, with: 'product.store_product'

  private

  def store_product(input)
    super(input) do |m|
      m.success { |out| Success([input, output]) }
      m.failure { |err| Failure(err) }
    end
  end
end

I want to test the method #store_product. So I want to inject the original operation by a mock operation.
Is it possible?


#2

Hey,

You can simply pass the mocked operation to your transaction constructor:

Rspec.describe Import do
  include Dry::Monads::Result::Mixin
  
  let(:import) { Import.new(store_product: ->(input) { Success(input) }

 it 'will work' do
  import.call('something')
 end
end

You can find more inspiration in the test code of the gem https://github.com/dry-rb/dry-transaction/blob/master/spec/integration/transaction_spec.rb


#3

I want to mock the ‘product.store_product’ operation in MyContainer, so that I can test the method store_product.

Your snippet mocks the method store_product.


#4

I believe you can stub MyContainer like here


#5

Thanks. But I don’t know how to take that stub to the Import class in this case.