title: ActiveRecord: Mock has_many relation call
body: <p>I'm fairly new to testing, so I've been struggling with using the correct syntax, especially regarding <code>mocks</code>.</p>
  
  <p>I want to test my <code>destroy</code> action in <strong>cars_controller.rb</strong></p>
  
  <pre><code>def destroy
    if current_user.cars.exists?(params[:id])
      car = current_user.cars.find(params[:id])
  
      # only destroy the car if it has no bookings
      car.destroy unless car.bookings.exists?
    end
  
    redirect_to user_cars_path(current_user)
  end
  </code></pre>
  
  <p>It was rather easy testing the case, when no bookings are associated with the car.</p>
  
  <pre><code>describe CarsController, type: :controller do
  
    let(:user) { create(:user_with_car) }
    before { login_user(user) }
  
    describe "DELETE #destroy" do
      let(:car) { user.cars.first }
  
      context "when the car has no bookings associated to it" do
        it "destroys the requested car" do
          expect {
            delete :destroy, user_id: user.id, id: car.id
          }.to change(user.cars, :count).by(-1)
        end
      end
  </code></pre>
  
  <p>But this test is driving me nuts:</p>
  
  <pre><code>    context "when the car has bookings associated to it" do
        it "does not destroy the requested car" do
  
          ##### This line fails miserably
          allow(car).to receive_message_chain(:bookings) { [ Booking.new ]}
  
          expect {
            delete :destroy, user_id: user.id, id: car.id
          }.to change(user.cars, :count).by(0)
        end
      end
    end
  end
  </code></pre>
  
  <p>I do not want to create bookings in the database and associate them with the car. As I understand, it is recommended to mock these bookings, since they have <em>no use</em> further on.</p>
  
  <p>Next to:</p>
  
  <pre><code>allow(car).to receive_message_chain(:bookings) { [ Booking.new ]}
  </code></pre>
  
  <p>I've had multiple attempts with other syntax', but all failed. I even tried using <a href="https://github.com/rspec/rspec-mocks" rel="nofollow">rpsec-mocks</a> old syntax: <code>stub(...)</code>.</p>
  
  <p>How would I accomplish this?</p>