Skip to content

Commit 0b02ede

Browse files
authored
Merge pull request rails#44833 from ghousemohamed/extend-test-cases-in-ar-for-methods-accepting-blocks
Extend test cases in AR for methods accepting blocks
2 parents c306358 + b3b781c commit 0b02ede

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

activerecord/test/cases/relations_test.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,18 @@ def test_create_with_array
13071307
green_birds.each { |bird| assert_predicate bird, :persisted? }
13081308
end
13091309

1310+
def test_create_with_block
1311+
sparrow = Bird.create do |bird|
1312+
bird.name = "sparrow"
1313+
bird.color = "grey"
1314+
end
1315+
1316+
assert_kind_of Bird, sparrow
1317+
assert_predicate sparrow, :persisted?
1318+
assert_equal "sparrow", sparrow.name
1319+
assert_equal "grey", sparrow.color
1320+
end
1321+
13101322
def test_create_bang_with_array
13111323
green_birds = Bird.where(color: "green").create!([{ name: "parrot" }, { name: "canary" }])
13121324
assert_equal ["parrot", "canary"], green_birds.map(&:name)
@@ -1481,6 +1493,19 @@ def test_find_or_create_by_with_create_with
14811493
assert_equal bird, Bird.create_with(color: "blue").find_or_create_by(name: "bob")
14821494
end
14831495

1496+
def test_find_or_create_by_with_block
1497+
assert_nil Bird.find_by(name: "bob")
1498+
1499+
bird = Bird.find_or_create_by(name: "bob") do |record|
1500+
record.color = "blue"
1501+
end
1502+
assert_predicate bird, :persisted?
1503+
assert_equal bird.name, "bob"
1504+
assert_equal bird.color, "blue"
1505+
1506+
assert_equal bird, Bird.find_or_create_by(name: "bob", color: "blue")
1507+
end
1508+
14841509
def test_find_or_create_by!
14851510
assert_raises(ActiveRecord::RecordInvalid) { Bird.find_or_create_by!(color: "green") }
14861511
end
@@ -1494,6 +1519,20 @@ def test_create_or_find_by
14941519
assert_not_equal subscriber, Subscriber.create_or_find_by(nick: "cat")
14951520
end
14961521

1522+
def test_create_or_find_by_with_block
1523+
assert_nil Subscriber.find_by(nick: "bob")
1524+
1525+
subscriber = Subscriber.create_or_find_by(nick: "bob") do |record|
1526+
record.name = "the builder"
1527+
end
1528+
1529+
assert_equal "bob", subscriber.nick
1530+
assert_equal "the builder", subscriber.name
1531+
assert_predicate subscriber, :persisted?
1532+
assert_equal subscriber, Subscriber.create_or_find_by(nick: "bob")
1533+
assert_not_equal subscriber, Subscriber.create_or_find_by(nick: "cat")
1534+
end
1535+
14971536
def test_create_or_find_by_should_not_raise_due_to_validation_errors
14981537
assert_nothing_raised do
14991538
bird = Bird.create_or_find_by(color: "green")
@@ -1562,6 +1601,20 @@ def test_find_or_initialize_by
15621601
assert_equal bird, Bird.find_or_initialize_by(name: "bob")
15631602
end
15641603

1604+
def test_find_or_initialize_by_with_block
1605+
assert_nil Bird.find_by(name: "bob")
1606+
1607+
bird = Bird.find_or_initialize_by(name: "bob") do |record|
1608+
record.color = "blue"
1609+
end
1610+
assert_predicate bird, :new_record?
1611+
assert_equal bird.name, "bob"
1612+
assert_equal bird.color, "blue"
1613+
bird.save!
1614+
1615+
assert_equal bird, Bird.find_or_initialize_by(name: "bob", color: "blue")
1616+
end
1617+
15651618
def test_explicit_create_with
15661619
hens = Bird.where(name: "hen")
15671620
assert_equal "hen", hens.new.name

0 commit comments

Comments
 (0)