我们如何使用 rspec 测试交互组织者?

How do we test interactor organizers using rspec?

我想测试下面的组织者交互器,调用 2 个指定的交互器而不执行调用交互器('SaveRecord, PushToService')代码。

class Create
  include Interactor::Organizer

  organize SaveRecord, PushToService
end

我发现很少有示例对所有交互逻辑(记录应保存并推送到其他服务)的总体结果进行了测试。但是,我不想执行其他交互器的逻辑,因为它们将作为其单独规范的一部分进行测试。

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?

我认为我们需要在不执行包含的交互器的情况下测试包含的交互器的交互器组织器。我能够找到一种方法存根并使用以下行测试组织者

存根:

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

测试:

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

从试图在内部调用和使用的交互器管理器源文件中找到 call! method & organized variable。存根 call! 方法并测试 organized 变量已满足我的要求。

您可以测试它们被调用的顺序:

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

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