views:

68

answers:

1

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.

A: 

You can mock the count too:

arr = [user1, user2]
arr.stubs(:count).returns(arr.size)
User.any_instance.stubs(:friends).returns(arr)
shingara
Sure, but I was looking for a general solution to this. count is only one of many methods injected into Array by rails.
Matthias