[rspec-users] stub_model() and ActiveRecord Associations

David Chelimsky dchelimsky at gmail.com
Wed Aug 27 08:32:01 EDT 2008


On Wed, Aug 27, 2008 at 2:34 AM, Matt Wynne <matt at mattwynne.net> wrote:
>> 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.

http://rubyurl.com/PHwC - it got started but a) the patch was never
completed and b) there are some mixed feelings about supporting it.
Feel free to add to the conversation in that ticket.

> You can't really mix mocking frameworks
> in RSpec, right?

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.
>
> cheers,
> Matt
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list