How to avoid using allow_any_instance_of? : <p>Imagine we have following piece of code:</p>

<pre><code>class A
  def create_server
    options = {
      name: NameBuilder.new.build_name
    }
    do_some_operations(options)
  end
end
</code></pre>

<p>To test such methods, I've used to use <code>allow_any_instance_of</code>:</p>

<pre><code>it 'does operations' do
  allow_any_instance_of(NameBuilder).to receive(:build_name)
  # test body
end
</code></pre>

<p>But docs advise us not to use it <a href="https://relishapp.com/rspec/rspec-mocks/docs/working-with-legacy-code/any-instance" rel="noreferrer">because of several reasons</a>. How then avoid <code>allow_any_instance_of</code>? I've came to only one solution: </p>

<pre><code>class A
  def create_server
    options = {
      name: builder.build_name
    }
    do_some_operations
  end

  private

  def builder
    NameBuilder.new
  end
end
</code></pre>

<p>But with such approach code quickly becomes full of almost useless methods (especially when you actively using composition of different objects in described class).</p>
