title: How to mock any_instance in a scope with RSpec?
body: <p>I'm trying to write a spec which expects a method to be called on all instances in a scope. Couldn't find a elegant way to do it.</p>
  
  <p>This is a simplified representation of my code:</p>
  
  <pre><code>class MyClass &lt; ActiveRecord::Base
  
  scope :active, where(:status =&gt; 'active')
  scope :inactive, where(:status =&gt; 'inactive')
  
  def some_action
    # some code
  end
  </code></pre>
  
  <p>This class is used by another class which calls <code>some_action</code> on <code>MyClass.all</code>:</p>
  
  <pre><code>class OtherClass
  
  def other_method
    MyClass.all.each do |item|
      item.some_action
    end
  end
  </code></pre>
  
  <p>I want to change it to:</p>
  
  <pre><code>class OtherClass
  
  def other_method
    MyClass.active.each do |item|
      item.some_action
    end
  end
  </code></pre>
  
  <p>To test such behavior I could simply <code>MyClass.stub(:active)</code>, return an Array of stubs and expect <code>some_action</code> on each stub. But I don't like that approach because it exposes too much details of the implementation.</p>
  
  <p>What I thing would be a little more elegant is something like <code>any_instance_in_scope</code>. Then I could simply write my spec as:</p>
  
  <pre><code>MyClass.any_instance_in_scope(:active).should_receive(:some_action)
  </code></pre>
  
  <p>Is there any way to achieve this?</p>