サービスクラスがHTTP呼び出しを処理し、アクションクラスがデータをサニタイズ/パッケージ化し、サービスクラスを呼び出して外部APIと通信するアクション/サービスベースのアーキテクチャを使用しています。セットアップはうまくいきますが、私はアクションクラスのモックを設定する方法が不思議です。ここでは例アクション/サービスのペアがあります:RSpecを使用してサービスクラスをインスタンス化するアクションクラスをテストする
# Service
class UserService
def update_user user_id, data
# make request
end
end
# Action
class UserUpdate
def initialize user_id, user_data
@id = user_id
@sanitized_data = user_data
end
def call
service = UserService.new
service.call(@id, @sanitized_data)
end
end
私は問題なく動作を模擬することができますが、私はサービスのための二重のクラスまたはインスタンスを使用しようとすると、私はエラーを取得します。ここに私のテストはこれまでのところです:私は思ったんだけど何私にFailure/Error: DEFAULT_FAILURE_NOTIFIER = lambda { |failure, _opts| raise failure } Exactly one instance should have received the following message(s) but didn't: initialize
をネット
it "should create an instance of Agent Service" do
agent_action_mock = class_double("AgentUpdate")
agent_service_mock = class_double("AgentService")
allow(agent_update_mock).to receive_message_chain(:new, :call)
allow_any_instance_of(AgentService).to receive_message_chain(:initialize, :update_user)
agenta_action_mock.new(99, { status: "Verified" }).call
expect_any_instance_of(AgentService).to receive(:initialize)
end
は、私はアクションは、サービスクラスのインスタンスを作成し、それのメソッドを呼び出していることをテストする方法ですか?
本当に本当に呼び出されていますか? –