[rspec-users] stub_model() and ActiveRecord Associations

David Chelimsky dchelimsky at gmail.com
Wed Aug 27 08:26:35 EDT 2008


On Wed, Aug 27, 2008 at 7:22 AM, Zach Dennis <zach.dennis at gmail.com> wrote:
> On Wed, Aug 27, 2008 at 3: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?
>
>
> It caches the SQL statements and their results. It uses the cached
> results to build an instance of your model. Although the identify of
> the objects are different, they are equal.
>
>    f = Foo.create :name => "blah"
>    f.equal?(Foo.last) # false
>    f == Foo.last       # true
>
> I don't know your ultimate goal,

He's trying to set a message expectation on an object that gets
returned by AR. Since AR builds a new object for each request, you
can't get a handle on it in the code example.

> but you could rely on object equality
> (and not identify equality) in your test:
>
>   @target_comment.should == @target_comments.first
>
> And then rely on @target_comments.first to set up your expectation for
> :merge_in.
>
> --
> Zach Dennis
> http://www.continuousthinking.com
> http://www.mutuallyhuman.com
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list