[英]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.