# How to write the method before\_destroy and the test

**URL:** <https://www.ruby-forum.com/t/how-to-write-the-method-before-destroy-and-the-test/141964>\
**Category:** Rails\
**Created:** [July 2, 2008, 3:30am UTC](https://www.ruby-forum.com/t/how-to-write-the-method-before-destroy-and-the-test/141964 "2008-07-02T03:30:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [July 2, 2008, 3:30am UTC](https://www.ruby-forum.com/t/how-to-write-the-method-before-destroy-and-the-test/141964/1 "2008-07-02T03:30:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [July 4, 2008, 2:25am UTC](https://www.ruby-forum.com/t/how-to-write-the-method-before-destroy-and-the-test/141964/2 "2008-07-04T02:25:17Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [July 4, 2008, 4:11pm UTC](https://www.ruby-forum.com/t/how-to-write-the-method-before-destroy-and-the-test/141964/3 "2008-07-04T16:11:47Z")

</div>

On Thu, Jul 3, 2008 at 8:23 PM, Ryan B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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
