[rspec-users] unexpected message expectations issue

David Chelimsky dchelimsky at gmail.com
Tue May 19 17:21:21 EDT 2009


On Tue, May 19, 2009 at 2:50 PM, Barun Singh <barunio at gmail.com> wrote:
> 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 ?

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

>
> Thanks...
>
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list