Forum: Ruby on Rails problem with assert_raise in Unit Test

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
D91bed43c57f64216edd0e887ebf9378?d=identicon&s=25 (Guest)
on 2006-12-31 00:46
(Received via mailing list)
I'm running a simple unit test to validate that ActiveRecord raises an
exception when I try and update a column with a duplicate foreign key.

Inside the unit test,

  charge.time_logs << time_logs(:first)

will generate

ActiveRecord::StatementInvalid: Mysql::Error: #23000Duplicate entry
'20' for key 2: UPDATE time_logs SET `task_id` = 1, `date_created` =
NULL, `time_charge_id` = 20, `hours` = 2.0, `log_entry` = NULL WHERE id
= 2


     assert_raise ActiveRecord::StatementInvalid do
       charge.time_logs << time_logs(:first)

will generate

<ActiveRecord::StatementInvalid> exception expected but none was

What am I doing wrong?
This topic is locked and can not be replied to.