title: Rspec - how to test that a nested service is instanciated appropriately and the appropriate method is called on this instance
body: <p>Suppose I have a class that is supposed to call a service multiple times with different arguments</p>
  
  <pre><code>class MyExecutor
    def perform
      targets.each do |target|
        MyService.new(target).call
      end
    end
  end
  
  class MyService
    def initialize(target)
      @target = target
    end
  
    def call
      @target.do_something
    end
  end
  </code></pre>
  
  <p>Assume I want to write a test on <code>MyExecutor</code>, generating data so that I have at least 2 targets, and I want to test that the service <code>MyService</code> is called appropriately on all targets.</p>
  
  <p>When I had only one target, I could use <code>expect_any_instance_of().to receive(:call)</code> but then I was not really testing the instanciation with appropriate params, and then this syntax is deprecated  (cf <a href="https://relishapp.com/rspec/rspec-mocks/docs/working-with-legacy-code/any-instance" rel="nofollow noreferrer">comment here</a>)</p>
  
  <pre><code>describe MyExecutor
    context 'with one target'
      it 'calls the MyService appropriately'
        expect_any_instance_of(MyService).to receive(:call)
        MyExecutor.perform
      end
    end
  end
  </code></pre>
  
  <p>Suppose I have multiple targets, <strong>how can I test that the MyService is  instanciated twice, once with each relevant target, and that on each of those instanciated services, the <code>call</code> method is called appropriately</strong> What is the proper non-deprecated way to test this ?</p>
  
  <p><em>Implicit question : (is this the right way to approach the problem ?)</em></p>