Forum: RSpec unexpected message expectations issue

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A70410682bd132d5edf76b42189ebb45?d=identicon&s=25 Barun Singh (barunio)
on 2009-05-19 22:06
(Received via mailing list)
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...
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-05-19 23:33
(Received via mailing list)
On Tue, May 19, 2009 at 2:50 PM, Barun Singh <barunio@gmail.com> 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.
This topic is locked and can not be replied to.