Assert_raises --> how does it work?

Hi,

I have this code fragment:

def test_delete_concept
assert_not_nil Concept.find(concepts(:entreprise).id)
get :delete_concept, {:concept_id => concepts(:entreprise).id}
assert_raises :RecordNotFound,
Concept.find(concepts(:entreprise).id)
end

basicaly, on the first line, I make sure that the concept exist in the
DB. On the second line I delete the concept fron the DB.

Finaly, this is where I have a problem, on the third line, I want to see
if the record was deleted. I though about using the assert_raises method
since a #find call to a non existant record would raise a
:RecordNotFound exception. But it doesnt work. The system simply tells
me that it can find a record with the provided ID and thats all… The
test fails on that line.

Any idea how I can do that test, or how I can use assert_raises?

Thanks!

You need:

assert_raises :RecordNotFound { Concept.find(concepts(:entreprise).id) }

since the code to be tested must be in a block - assert_raises will
perform a yield on that block.

Alain Pilon wrote:

Hi,

I have this code fragment:

def test_delete_concept
assert_not_nil Concept.find(concepts(:entreprise).id)
get :delete_concept, {:concept_id => concepts(:entreprise).id}
assert_raises :RecordNotFound,
Concept.find(concepts(:entreprise).id)
end

basicaly, on the first line, I make sure that the concept exist in the
DB. On the second line I delete the concept fron the DB.

Finaly, this is where I have a problem, on the third line, I want to see
if the record was deleted. I though about using the assert_raises method
since a #find call to a non existant record would raise a
:RecordNotFound exception. But it doesnt work. The system simply tells
me that it can find a record with the provided ID and thats all… The
test fails on that line.

Any idea how I can do that test, or how I can use assert_raises?

Thanks!

Thanks Mick but it doesnt work. I tred that line:

assert_raises :RecordNotFound { Concept.find(concepts(:entreprise).id)
}

and got that message:

generic_controller_test.rb:46: parse error, unexpected ‘{’, expecting
kEND
assert_raises :RecordNotFound {
Concept.find(concepts(:entreprise).id) }
^
generic_controller_test.rb:46: parse error, unexpected ‘}’, expecting
kEND
generic_controller_test.rb:97: parse error, unexpected $, expecting kEND

any idea?

thanks

Hmmn…asert_raises appears to be deprecated, and I can’t find any
documentation on it. Try using assert_raise instead, and put the
argument in parentheses. Also, make an explicit class reference to
RecordNotFound, thus:

assert_raise(ActiveRecord::RecordNotFound) {
Concept.find(concepts(:entreprise).id)
}

If you still get syntax errors, it may be to do with some earlier code.

HTH, Mick

Alain Pilon wrote:

Thanks Mick but it doesnt work. I tred that line:

assert_raises :RecordNotFound { Concept.find(concepts(:entreprise).id)
}

and got that message:

generic_controller_test.rb:46: parse error, unexpected ‘{’, expecting
kEND
assert_raises :RecordNotFound {
Concept.find(concepts(:entreprise).id) }
^
generic_controller_test.rb:46: parse error, unexpected ‘}’, expecting
kEND
generic_controller_test.rb:97: parse error, unexpected $, expecting kEND

any idea?

thanks

I haven’t looked at it in any detail yet, but they have added a section
on the new integration testing facilities.

Mick

Alain Pilon wrote:

Thanks, its working now!

Received the same tip from Jeremy, so thanks to you too! Do you know if
the new Agile dev on RoR book from pragmatic talk about all the changes
affecting testing? Or if they devote more pages to it?

Thanks, its working now!

Received the same tip from Jeremy, so thanks to you too! Do you know if
the new Agile dev on RoR book from pragmatic talk about all the changes
affecting testing? Or if they devote more pages to it?

On May 6, 2006, at 7:18 PM, Alain Pilon wrote:

assert_raises :RecordNotFound {

Concept.find(concepts(:entreprise).id) }
^
generic_controller_test.rb:46: parse error, unexpected ‘}’, expecting
kEND
generic_controller_test.rb:97: parse error, unexpected $, expecting
kEND

any idea?

{ and do have different precedence, { binds to the closest while do
to the farthest. Switch { } for do end or add parens to bind it to
the method call.


Eric H. - [email protected] - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com