简体   繁体   中英

How do we test interactor organizers using rspec?

I want to test below organizer interactor for, calling the 2 specified interactors without executing the calling interactors('SaveRecord, PushToService') code.

class Create
  include Interactor::Organizer

  organize SaveRecord, PushToService
end

I found few examples where the overall result of all the interactors logic(record should be saved and pushed to other service) has been tested. But, i dont want to execute the other interactor's logic as they will be tested as part of their separate specs.

1. Is it possible to do so?
2. Which way of testing(testing the overall result/testing only this particular 
   organizer interactor behavior) is a better practise?

I believe we need to test the interactor organizer for included interactors without executing the included interacors. I am able to find a way stub and test the organizer with below lines

To Stub:

  allow(SaveRecord).to receive(:call!) { :success }
  allow(PushToService).to receive(:call!) { :success }

To Test:

it { expect(interactor).to be_kind_of(Interactor::Organizer) }
it { expect(described_class.organized).to eq([SaveRecord, PushToService]) }

Found call! method & organized variable call! method & organized variable from interactor organizer source files where it is trying to call and use internally. Stubbing the call! method and testing the organized variable has fulfilled my requirement.

You can test they are called and the order:

it 'calls the interactors' do
  expect(SaveRecord).to receive(:call!).ordered
  expect(PushToService).to receive(:call!).ordered
  described_class.call
end

See: https://relishapp.com/rspec/rspec-mocks/docs/setting-constraints/message-order

Just iterating over @prem answer.

To Test:

 it { expect(interactor).to be_kind_of(Interactor::Organizer) } it { expect(described_class.organized).to eq([SaveRecord, PushToService]) }

interactor in this case is an instance of the Interactor class, or in Rspec syntax:

let(:interactor) { described_class.new }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM