aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations/has_one_associations_test.rb
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-01-08 19:59:30 +0000
committerAaron Patterson <aaron.patterson@gmail.com>2011-01-11 13:45:07 -0800
commit7f7b480098fa780dd76b4c1243230d74be67b3ca (patch)
treec94d3876cd75372e07c7b050e43508f85ded2183 /activerecord/test/cases/associations/has_one_associations_test.rb
parent1bc71ed9607ba88c21c14b670a4308c8549f3941 (diff)
downloadrails-7f7b480098fa780dd76b4c1243230d74be67b3ca.tar.gz
rails-7f7b480098fa780dd76b4c1243230d74be67b3ca.tar.bz2
rails-7f7b480098fa780dd76b4c1243230d74be67b3ca.zip
When assigning a has_one, if the new record fails to save, raise an error
Diffstat (limited to 'activerecord/test/cases/associations/has_one_associations_test.rb')
-rw-r--r--activerecord/test/cases/associations/has_one_associations_test.rb28
1 files changed, 14 insertions, 14 deletions
diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb
index 46459df7c5..b9719fa983 100644
--- a/activerecord/test/cases/associations/has_one_associations_test.rb
+++ b/activerecord/test/cases/associations/has_one_associations_test.rb
@@ -93,18 +93,18 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
def test_nullification_on_association_change
firm = companies(:rails_core)
old_account_id = firm.account.id
- firm.account = Account.new
+ firm.account = Account.new(:credit_limit => 5)
# account is dependent with nullify, therefore its firm_id should be nil
assert_nil Account.find(old_account_id).firm_id
end
def test_association_change_calls_delete
- companies(:first_firm).deletable_account = Account.new
+ companies(:first_firm).deletable_account = Account.new(:credit_limit => 5)
assert_equal [], Account.destroyed_account_ids[companies(:first_firm).id]
end
def test_association_change_calls_destroy
- companies(:first_firm).account = Account.new
+ companies(:first_firm).account = Account.new(:credit_limit => 5)
assert_equal [companies(:first_firm).id], Account.destroyed_account_ids[companies(:first_firm).id]
end
@@ -182,17 +182,6 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
assert_equal account, firm.account
end
- def test_failing_build_association
- firm = Firm.new("name" => "GlobalMegaCorp")
- firm.save
-
- firm.account = account = Account.new
- assert_equal account, firm.account
- assert !account.save
- assert_equal account, firm.account
- assert_equal ["can't be empty"], account.errors["credit_limit"]
- end
-
def test_create
firm = Firm.new("name" => "GlobalMegaCorp")
firm.save
@@ -320,4 +309,15 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
assert_equal ships(:black_pearl), pirate.ship
assert_equal pirate.id, pirate.ship.pirate_id
end
+
+ def test_replacement_failure_due_to_new_record_should_raise_error
+ pirate = pirates(:blackbeard)
+ new_ship = Ship.new
+
+ assert_raise(ActiveRecord::RecordNotSaved) do
+ pirate.ship = new_ship
+ end
+ assert_equal new_ship, pirate.ship
+ assert_equal pirate.id, new_ship.pirate_id
+ end
end