From 55aab5b8acb72967228dc38f6432e0f126a94d77 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 9 Oct 2006 01:52:24 +0000 Subject: Added update_attributes! which uses save! to raise an exception if a validation error prevents saving (closes #6192) [jonathan] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5256 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/test/base_test.rb | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb index 9a67426ba2..e5eabc57ab 100755 --- a/activerecord/test/base_test.rb +++ b/activerecord/test/base_test.rb @@ -149,8 +149,11 @@ class BasicsTest < Test::Unit::TestCase def test_save! topic = Topic.new(:title => "New Topic") assert topic.save! - end + reply = Reply.new + assert_raise(ActiveRecord::RecordInvalid) { reply.save! } + end + def test_hashes_not_mangled new_topic = { :title => "New Topic" } new_topic_values = { :title => "AnotherTopic" } @@ -646,6 +649,40 @@ class BasicsTest < Test::Unit::TestCase assert !Topic.find(1).approved? end + def test_update_attributes + topic = Topic.find(1) + assert !topic.approved? + assert_equal "The First Topic", topic.title + + topic.update_attributes("approved" => true, "title" => "The First Topic Updated") + topic.reload + assert topic.approved? + assert_equal "The First Topic Updated", topic.title + + topic.update_attributes(:approved => false, :title => "The First Topic") + topic.reload + assert !topic.approved? + assert_equal "The First Topic", topic.title + end + + def test_update_attributes! + reply = Reply.find(2) + assert_equal "The Second Topic's of the day", reply.title + assert_equal "Have a nice day", reply.content + + reply.update_attributes!("title" => "The Second Topic's of the day updated", "content" => "Have a nice evening") + reply.reload + assert_equal "The Second Topic's of the day updated", reply.title + assert_equal "Have a nice evening", reply.content + + reply.update_attributes!(:title => "The Second Topic's of the day", :content => "Have a nice day") + reply.reload + assert_equal "The Second Topic's of the day", reply.title + assert_equal "Have a nice day", reply.content + + assert_raise(ActiveRecord::RecordInvalid) { reply.update_attributes!(:title => nil, :content => "Have a nice evening") } + end + def test_mass_assignment_protection firm = Firm.new firm.attributes = { "name" => "Next Angle", "rating" => 5 } -- cgit v1.2.3