title: Using RSpec to test an action class that instantiates a service class
body: <p>I'm using an action/service based architecture where a service class hosts HTTP calls and action classes sanitize/package data and call the service class to talk to an outside API. The setup works fine, but I'm wondering how to set up mocks for the action classes. Here's an example action/service pair:</p>
  
  <pre><code># 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
  </code></pre>
  
  <p>I can mock the action without issue, but when I try to use a class or instance double for the service, I get errors. Here's my test so far:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Which nets me <code>Failure/Error: DEFAULT_FAILURE_NOTIFIER = lambda { |failure, _opts| raise failure } Exactly one instance should have received the following message(s) but didn't: initialize</code></p>
  
  <p>What I'm wondering is how can I test that the action is creating an instance of the service class and calling it's methods?</p>