[rspec-users] stub_model() and ActiveRecord Associations

Matt Wynne matt at mattwynne.net
Wed Aug 27 03:34:58 EDT 2008

> Here's the basic deal:
> Model.find(1).equal?(Model.find(1))
> => false
> AR does not cache objects, so when you ask it for what you *think*
> might the same object twice, you get different ones.

I thought as much... So does AR just cache the object's attributes  
instead and construct them on the fly as and when you ask for them?

> mocha offers an 'any_instance' method, which gives you basically what
> you are describing with should_receive_instance_method, but rspec does
> not yet have a counterpart in its mocking library.

Interesting. I'd love to dive in and write one for RSpec but I think  
it might be a bit beyond me right now. You can't really mix mocking  
frameworks in RSpec, right?

> I can tell you how I handle this, but let me say that this is a less
> than perfect way to handle a less than perfect situation so it's
> difficult for me to say "this is a recommended approach." That said
> ...
> @target_comment = stub_model(Target)
> @target.stub!(:comments).and_return([@target_comment])

Bugger - I was hoping you wouldn't say that :)

Okay well at least I know what the deal is. Thanks as usual David.


More information about the rspec-users mailing list