Skip to content

Commit

Permalink
Merge pull request #42291 from martinjaimem/enhancement/add-tests-for…
Browse files Browse the repository at this point in the history
…-error-messages

Add tests for argument error messages in ActiveRecord methods
  • Loading branch information
kamipo committed May 26, 2021
2 parents 93e7343 + eec0ff0 commit 56cfefe
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
19 changes: 16 additions & 3 deletions activerecord/test/cases/persistence_test.rb
Expand Up @@ -307,12 +307,25 @@ def test_create
assert_equal("New Topic", topic_reloaded.title)
end

def test_save!
def test_save_valid_record
topic = Topic.new(title: "New Topic")
assert topic.save!
end

def test_save_invalid_record
reply = WrongReply.new(title: "New reply")
error = assert_raise(ActiveRecord::RecordInvalid) { reply.save! }

assert_equal "Validation failed: Content Empty", error.message
end

def test_save_destroyed_object
topic = Topic.create!(title: "New Topic")
topic.destroy!

error = assert_raise(ActiveRecord::RecordNotSaved) { topic.save! }

reply = WrongReply.new
assert_raise(ActiveRecord::RecordInvalid) { reply.save! }
assert_equal "Failed to save the record", error.message
end

def test_save_null_string_attributes
Expand Down
4 changes: 3 additions & 1 deletion activerecord/test/cases/relation/update_all_test.rb
Expand Up @@ -39,7 +39,9 @@ def test_update_all_with_non_standard_table_name
end

def test_update_all_with_blank_argument
assert_raises(ArgumentError) { Comment.update_all({}) }
error = assert_raises(ArgumentError) { Comment.update_all({}) }

assert_equal "Empty list of attributes to change", error.message
end

def test_update_all_with_joins
Expand Down
5 changes: 4 additions & 1 deletion activerecord/test/cases/scoping/relation_scoping_test.rb
Expand Up @@ -425,9 +425,12 @@ def test_raises_error_if_all_queries_is_set_to_false_while_nested
select_sql = capture_sql { Author.first }.first
assert_match(/organization_id/, select_sql)

assert_raises ArgumentError do
error = assert_raises ArgumentError do
Author.where(organization_id: 1).scoping(all_queries: false) { }
end

assert_equal "Scoping is set to apply to all queries and cannot be " \
"unset in a nested block.", error.message
end
end
end
Expand Down

0 comments on commit 56cfefe

Please sign in to comment.