Forum: Ruby on Rails How to test :dependent => :destroy

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.
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2009-04-16 12:03
Hi there :-)

I wanted to know what's a good way to test whether dependent objects are
really destroyed when destroying the parent object?

I have a Blog model which has_many Comment model children. These
comments are set to :dependent => :destroy.

What's a good way to check this functionality in a unit test?

Thanks a lot
Josh
4375b4aca04b8d72482fd409fb6d0b47?d=identicon&s=25 Roderick van Domburg (roderickvd)
on 2009-04-16 12:04
Joshua Muheim wrote:
> I wanted to know what's a good way to test whether dependent objects are
> really destroyed when destroying the parent object?
>
> I have a Blog model which has_many Comment model children. These
> comments are set to :dependent => :destroy.
>
> What's a good way to check this functionality in a unit test?

Shortly:

assert_difference 'Comment.count' do
  blog(:with_comments).destroy
end

--
Roderick van Domburg
http://www.nedforce.nl
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2009-04-16 12:19
Thank you so far! I tried the following:

    blog = blogs(:first_blog)
    assert_difference Comment, :count, blog.comments.size do
      assert blog.destroy
    end

Then I get the error:

TypeError: can't convert Class into String

So I changed Comment to 'Comment':

    assert_difference 'Comment', :count, blog.comments.size do
      assert blog.destroy
    end

NoMethodError: undefined method `+' for #<Class:0x22cb198>

And that doesn't make any sense to me... ;-) Any help? :-)
059ed46172a087063ce26250e44c8627?d=identicon&s=25 Fernando Perez (fernando)
on 2009-04-16 12:27
> NoMethodError: undefined method `+' for #<Class:0x22cb198>
Somewhere in your Blog or Comment code, there is a + hanging around that
is trying to add an apple to a car engine.
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2009-04-16 12:29
Fernando Perez wrote:
>
>> NoMethodError: undefined method `+' for #<Class:0x22cb198>
> Somewhere in your Blog or Comment code, there is a + hanging around that
> is trying to add an apple to a car engine.

No idea what you mean... Here's the code:

class Blog < ActiveRecord::Base
  validates_presence_of :user_id, :title, :content

  belongs_to :user

  has_many :comments,
    :as => :commentable,
    :dependent => :destroy
end

class Comment < ActiveRecord::Base
  validates_presence_of :commentable,
                        :subject,
                        :body

  belongs_to :user
  belongs_to :article
  belongs_to :commentable, :polymorphic => true # Kann sowohl einem
Article als auch einem Blog zugehörig sein

  def html_anchor
    "comment-#{id}"
  end
end
648a71a35ce6e4a1c5cd63451a2a20df?d=identicon&s=25 Gavin (Guest)
on 2009-04-16 12:45
(Received via mailing list)
have you tried:

assert_difference "Comment.count", blog.comments.count do
  blog(:with_comments).destroy
end

might need to put a minus before the blog.comments.count too.



On Apr 16, 11:29 am, Joshua Muheim <rails-mailing-l...@andreas-s.net>
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2009-04-16 12:47
Gavin wrote:
> have you tried:
>
> assert_difference "Comment.count", blog.comments.count do
>   blog(:with_comments).destroy
> end
>
> might need to put a minus before the blog.comments.count too.
>
>
>
> On Apr 16, 11:29�am, Joshua Muheim <rails-mailing-l...@andreas-s.net>

Worked perfectly, the minus was necessary, too. Thanks a lot :-)
648a71a35ce6e4a1c5cd63451a2a20df?d=identicon&s=25 Gavin (Guest)
on 2009-04-16 12:59
(Received via mailing list)
:)
This topic is locked and can not be replied to.