Skip to content

Commit 65ee31b

Browse files
committed
Use assert_raises(match:) for lock_version: nil tests
1 parent 1edb808 commit 65ee31b

File tree

2 files changed

+9
-16
lines changed

2 files changed

+9
-16
lines changed

activerecord/lib/active_record/locking/optimistic.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def _update_row(attribute_names, attempted_action = "update")
103103

104104
if self[locking_column].nil?
105105
raise(<<-MSG.squish)
106-
For optimistic locking, '#{locking_column}' should not be set to `nil`/`NULL`.
106+
For optimistic locking, locking_column ('#{locking_column}') can't be nil.
107107
Are you missing a default value or validation on '#{locking_column}'?
108108
MSG
109109
end

activerecord/test/cases/locking_test.rb

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -307,24 +307,14 @@ def test_touch_existing_lock_without_default_should_work_with_null_in_the_databa
307307

308308
def test_update_lock_version_to_nil_without_validation_or_constraint_raises_error
309309
t1 = LockWithoutDefault.create!(title: "title1")
310-
error = assert_raises(RuntimeError) { t1.update(lock_version: nil) }
311-
312-
assert_match(locking_column_nil_error_message, error.message)
313-
314-
error = assert_raises(RuntimeError) { t1.update!(lock_version: nil) }
315-
316-
assert_match(locking_column_nil_error_message, error.message)
310+
assert_raises(RuntimeError, match: locking_column_nil_error_message) { t1.update(lock_version: nil) }
311+
assert_raises(RuntimeError, match: locking_column_nil_error_message) { t1.update!(lock_version: nil) }
317312
end
318313

319314
def test_update_lock_version_to_nil_without_validation_raises
320315
person = Person.find(1)
321-
error = assert_raises(RuntimeError) { person.update(lock_version: nil) }
322-
323-
assert_match(locking_column_nil_error_message, error.message)
324-
325-
error = assert_raises(RuntimeError) { person.update!(lock_version: nil) }
326-
327-
assert_match(locking_column_nil_error_message, error.message)
316+
assert_raises(RuntimeError, match: locking_column_nil_error_message) { person.update(lock_version: nil) }
317+
assert_raises(RuntimeError, match: locking_column_nil_error_message) { person.update!(lock_version: nil) }
328318
end
329319

330320
def test_update_lock_version_to_nil_with_validation_does_not_raise_runtime_lock_version_error
@@ -597,7 +587,10 @@ def test_yaml_dumping_with_lock_column
597587

598588
private
599589
def locking_column_nil_error_message
600-
/'lock_version' should not be set to `nil`/
590+
<<-MSG.squish
591+
For optimistic locking, locking_column ('lock_version') can't be nil.
592+
Are you missing a default value or validation on 'lock_version'?
593+
MSG
601594
end
602595
end
603596

0 commit comments

Comments
 (0)