title: How to stub an active record relation to test a where clause with rspec?
body: <p>I've got a class that look like this:</p>
  
  <pre><code>class Foo &lt; ActiveRecrod::Base
    has_many :bars
  
    def nasty_bars_present?
      bars.where(bar_type: "Nasty").any?
    end
  
    validate :validate_nasty_bars
    def validate_nasty_bars
      if nasty_bars_present?
        errors.add(:base, :nasty_bars_present)
      end
    end
  end
  </code></pre>
  
  <p>In testing the #nasty_bars_present? I'd method like to write an rspec test that stubs the bars association but allows the where to execute naturally. Something like:</p>
  
  <pre><code>describe "#nasty_bars_present?" do
    context "with nasty bars" do
      before { foo.stub(:bars).and_return([mock(Bar, bar_type: "Nasty")]) }
      it "should return true" do
        expect(foo.nasty_bars_present?).to be_true
      end
    end
  end
  </code></pre>
  
  <p>The test above gives an error about there being no method where for an array. How can I wrap the mock so the where will execute appropriately?</p>
  
  <p>Thanks!</p>