From dd120ede53eaf71dee76894998a81626b7a689fc Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 30 Apr 2008 23:14:32 -0500 Subject: Added block-setting of attributes for Base.create like Base.new already has (Adam Meehan) [#39 state:resolved] --- activerecord/test/cases/base_test.rb | 21 +++++++++++++++++++++ activerecord/test/cases/validations_test.rb | 16 ++++++++++++++++ 2 files changed, 37 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 93719c710a..e07ec50c46 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -251,6 +251,27 @@ class BasicsTest < ActiveRecord::TestCase topic = Topic.create("title" => "New Topic") topicReloaded = Topic.find(topic.id) assert_equal(topic, topicReloaded) + end + + def test_create_through_factory_with_block + topic = Topic.create("title" => "New Topic") do |t| + t.author_name = "David" + end + topicReloaded = Topic.find(topic.id) + assert_equal("New Topic", topic.title) + assert_equal("David", topic.author_name) + end + + def test_create_many_through_factory_with_block + topics = Topic.create([ { "title" => "first" }, { "title" => "second" }]) do |t| + t.author_name = "David" + end + assert_equal 2, topics.size + topic1, topic2 = Topic.find(topics[0].id), Topic.find(topics[1].id) + assert_equal "first", topic1.title + assert_equal "David", topic1.author_name + assert_equal "second", topic2.title + assert_equal "David", topic2.author_name end def test_update diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb index ca36ad3581..e3ca8660ac 100755 --- a/activerecord/test/cases/validations_test.rb +++ b/activerecord/test/cases/validations_test.rb @@ -133,6 +133,22 @@ class ValidationsTest < ActiveRecord::TestCase Reply.create!([ { "title" => "OK" }, { "title" => "Wrong Create" }]) end end + + def test_exception_on_create_bang_with_block + assert_raises(ActiveRecord::RecordInvalid) do + Reply.create!({ "title" => "OK" }) do |r| + r.content = nil + end + end + end + + def test_exception_on_create_bang_many_with_block + assert_raises(ActiveRecord::RecordInvalid) do + Reply.create!([{ "title" => "OK" }, { "title" => "Wrong Create" }]) do |r| + r.content = nil + end + end + end def test_scoped_create_without_attributes Reply.with_scope(:create => {}) do -- cgit v1.2.3