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