Blog post -

Rails Gotcha: Mocking/Stubbing on an Associated ActiveRecord Object

In RSpec it is still not possible to have mocks or stubs on all instances of a class, although this has been suggested and it is a feature of the Mocha framework. Stubbing or mocking an object returned by an ActiveRecord belongs_to association (i.e. article.author) won't do what you expect. Instead of mocking the associated object you end up mocking the AssociationProxy object that wraps the associated object. To work around this you can use the AssociationProxy#proxy_target method, something like this:

gist

So when we think we are working directly with an ActiveRecord model, we are in fact working with a proxy object, and this can be confusing:

gist

As far as I can tell this behaviour won't change with Rails 3 since its AssociationProxy class works the same way.