I've run into this problem with testing. Let's assume I have two models, User and Post, where user has_many :posts.
I'm trying to spec out a code block that includes something like this:
user = User.find(123)
post = user.posts.find(456)
I know how to mock out the User.find
and user.posts
parts. The user.posts
mock returns an array of Post objects. And when it get's to .find(456)
part, everything breaks down with no block given
exception.
So my question here is: what do I return as the result of the user.posts
mock, so that .find(456)
method works on it? User.first.posts.class
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.
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.