简体   繁体   English

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

[英]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.我想测试下面的组织者交互器,调用 2 个指定的交互器而不执行调用交互器('SaveRecord,PushToService')代码。

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 call! method & organized variable from interactor organizer source files where it is trying to call and use internally.来自交互器组织器源文件的call! method & organized variable ,它试图在内部调用和使用。 Stubbing the call!堵住call! method and testing the organized variable has fulfilled my requirement.方法和测试organized变量已满足我的要求。

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请参阅: https : //relishapp.com/rspec/rspec-mocks/docs/setting-constraints/message-order

Just iterating over @prem answer.只是迭代@prem 的回答。

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:在这种情况下, interactor是 Interactor 类的一个实例,或者在 Rspec 语法中:

let(:interactor) { described_class.new }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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