How to write the method before_destroy? Here is my code: def before_destroy errors.add(:isdefault,"Can''t destroy default record") if self.isdefault return false end or I should write it like below? def before_destroy raise(Can''t destroy default record") if self.isdefault end and how to write the test for it -- 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 -~----------~----~----~----~------~----~------~--~---
You don''t write tests for rails methods firstly. Secondly, errors.add(:isdefault, "Can''t destroy default record") and return false if isdefault? You put the return false after this if statement so it would always return false! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Craig Demyanovich
2008-Jul-04 14:11 UTC
Re: How to write the method before_destroy and the test
On Thu, Jul 3, 2008 at 8:23 PM, Ryan Bigg <radarlistener-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > You don''t write tests for rails methods firstly.To elaborate on this statement a little, you don''t want to test the framework, i.e., you don''t want to test that destroy actually destroys the model. That''s already well-tested by the framework. Rather, focus your tests on the conditional behavior that you''re adding around destroying the model, which is that destroy doesn''t destroy the model if it''s the default record. For example, def test_cannot_destroy_default_record some_model = SomeModel.create! :isdefault => true some_model.destroy # make sure we get an error and/or we can still find the record end By focusing on what destroy does when :isdefault is true instead of how it works (that before_destroy is used to check :isdefault), your tests can be fairly immune to how you implement this behavior. If someone wants to fill in the last line of the test, please do share. Regards, Craig --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---