Unexpected message expectations issue


#1

Suppose a User has many Items, and I want to spec that when a user is
destroyed its associated items are destroyed. I’m seeing some
unexpected
behavior:

describe User do
before(:each) do
@user = create_user # helper method that actually creates & saves
a
user
@item = create_item
@user.items << @item
end

THIS WORKS

it “should delete associated items when a user is destroyed” do
@item.should == @user.items[0]
@user.items[0].should_receive(:destroy)
@user.destroy
end

THIS FAILS

it “should delete associated items when a user is destroyed” do
@item.should_receive(:destroy)
@user.destroy
end
end

Why does the message expectation fail on @item but succeed on
@user.items[0], even though @user.items[0] == @item ?

Thanks…


#2

On Tue, May 19, 2009 at 2:50 PM, Barun S. removed_email_address@domain.invalid wrote:

end
@item.should_receive(:destroy)
@user.destroy
end
end

Why does the message expectation fail on @item but succeed on
@user.items[0], even though @user.items[0] == @item ?

But @user.items[0].equal?(@item) wiil return false. They’re not the
same object. One of the mysteries of AR associations and proxies.