# Testing which RuntimeError is raised

**URL:** <https://www.ruby-forum.com/t/testing-which-runtimeerror-is-raised/98793>\
**Category:** Rails\
**Created:** [July 18, 2006, 7:30am UTC](https://www.ruby-forum.com/t/testing-which-runtimeerror-is-raised/98793 "2006-07-18T07:30:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cynthia\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@Cynthia\_K](https://www.ruby-forum.com/u/Cynthia_K)\
**Post date:** [July 18, 2006, 7:30am UTC](https://www.ruby-forum.com/t/testing-which-runtimeerror-is-raised/98793/1 "2006-07-18T07:30:30Z")

</div>

I am just starting to use unit testing. Way cool. But I am not finding  
very clear docs on assert\_raise. I have managed to write a test that  
tells me that the destroy I am trying to test fails with a  
RuntimeError (as it should). However, I would really like to test that  
it rails for the specific reason I have in my model. How can I get  
more specific? Am I going to have to create my own exception class so  
I have a named exception to check for? What I have a the moment is:

In my model file:

# don’t destroy the home page (id = 1)

before\_destroy :dont\_destroy\_homepage

def dont\_destroy\_homepage  
raise “You can’t delete the home page or you will get rid of your  
entire site” if id == 1  
end

Then in my test file:

def test\_cant\_destroy\_homepage  
homepage = pages(:homepage)  
assert\_equal homepage.id, 1  
# I would really love to figure out how to test what RuntimeError is  
raised  
assert\_raise(RuntimeError) {homepage.destroy}  
end

---

<div class="post-metadata">

**Author:** ![Cynthia\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@Cynthia\_K](https://www.ruby-forum.com/u/Cynthia_K)\
**Post date:** [July 18, 2006, 9:36am UTC](https://www.ruby-forum.com/t/testing-which-runtimeerror-is-raised/98793/2 "2006-07-18T09:36:38Z")

</div>

exception = assert\_raise(RuntimeError) {homepage.destroy}  
puts exception.message

(I think)

Chris

---

<div class="post-metadata">

**Author:** ![Cynthia\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@Cynthia\_K](https://www.ruby-forum.com/u/Cynthia_K)\
**Post date:** [July 18, 2006, 11:29pm UTC](https://www.ruby-forum.com/t/testing-which-runtimeerror-is-raised/98793/3 "2006-07-18T23:29:29Z")

</div>

On 7/18/06, Chris R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> exception = assert\_raise(RuntimeError) {homepage.destroy}  
> puts exception.message

Thanks Chris. That does indeed print the message to the console - and  
told me how to access the message. I altered my test to then assert  
the message was what I wanted:

def test\_cant\_destroy\_homepage  
homepage = pages(:homepage)  
assert\_equal homepage.id, 1  
problem = assert\_raise(RuntimeError) {homepage.destroy}  
assert\_equal “You can’t delete the home page”, problem.message  
end

And I was please to note that the assert\_raise still counted in my  
number of assertions.
