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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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? :-) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 :-) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
:) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---