For RSpec 2.14.1 (it should also work for RSpec 3.1), I would try this:
describe "#nasty_bars_present?" do context "with nasty bars" do before :each do foo = Foo.new bar = double("Bar") allow(bar).to receive(:where).with({bar_type: "Nasty"}).and_return([double("Bar", bar_type: "Nasty")]) allow(foo).to receive(:bars).and_return(bar) end it "should return true" do expect(foo.nasty_bars_present?).to be_true end end end
Thus, if you call bars.where(bar_type: "Nasty") without specific conditions in the where statement, you will not get a double line with bar_type: "Nasty" . It can be reused for future mockery of the bars (at least to return one instance, for multiple instances, you would add another double).
source share