[英]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! 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
只是迭代@prem 的回答。
去测试:
it { expect(interactor).to be_kind_of(Interactor::Organizer) } it { expect(described_class.organized).to eq([SaveRecord, PushToService]) }
在这种情况下, interactor
是 Interactor 类的一个实例,或者在 Rspec 语法中:
let(:interactor) { described_class.new }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.