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.
The issue is that
user.postsisn’t actually a simpleArray; it’s an association proxy object. The way to stub it is probably something like this (though the exact syntax depends on which mocking framework you’re using):Then in your test,
User.find(123)will return@userand@user.posts.find(456)will return@post. If you need@user.poststo act like more of theArrayin your tests you can create amock(Array)and stub the[](index)method.