title: Mocking ActiveRecord relationship beheavior in RSpec tests
body: <p>I've run into this problem with testing. Let's assume I have two models, User and Post, where user has_many :posts.</p>
  
  <p>I'm trying to spec out a code block that includes something like this:</p>
  
  <pre><code>user = User.find(123)
  post = user.posts.find(456)
  </code></pre>
  
  <p>I know how to mock out the <code>User.find</code> and <code>user.posts</code> parts. The <code>user.posts</code> mock returns an array of Post objects. And when it get's to <code>.find(456)</code> part, everything breaks down with <code>no block given</code> exception.</p>
  
  <p>So my question here is: what do I return as the result of the <code>user.posts</code> mock, so that <code>.find(456)</code> method works on it? <code>User.first.posts.class</code> says it's Array, but obviously there's something more that makes the AR-style find calls work. I'm not overjoyed by the prospect of mocking out find method on the returned object.</p>
  
  <p>PS Before you suggest the obvious and good answer of stop mocking about and using fixtures/seeding the test database with necessary data, here's the catch: legacy scheme. Both User and Post work on top of database views not tables, and changing it so that they are tables in test database seems wrong to me.</p>