Stubbing ActiveRecord result arrays

Posted by Matthias on Stack Overflow See other posts from Stack Overflow or by Matthias
Published on 2010-03-25T14:25:06Z Indexed on 2010/03/25 16:23 UTC
Read the original article Hit count: 428

Filed under:
|
|
|

Using Ruby 1.8.6, stubbing a finder method to return an Array does not work when the code under test calls the 'count' method on the result:

User.any_instance.stubs(:friends).returns([user1, user2])

That's because Array.count has only been added in Ruby 1.8.7. Rails also adds a count method dynamically, I believe through the ActiveRecord::Calculations module.

Is there any way to turn this result array into something that behaves exactly as the special Array kind returned by a Rails finder method? When paginating results, it's easy: I can simply call [].paginate. But that doesn't work with normal finder results. I tried [].extend(ActiveRecord::Calculations) but that doesn't work either.

© Stack Overflow or respective owner

Related posts about rails

Related posts about testing