Skip to content

Commit

Permalink
Merge pull request #47563 from p8/activerecord/improve-attributes-met…
Browse files Browse the repository at this point in the history
…hods-test

Improve tests for read_attribute when attribute is missing
  • Loading branch information
guilleiguaran committed Mar 3, 2023
2 parents 068503d + 43b9386 commit 556025d
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions activerecord/test/cases/attribute_methods_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,12 @@ def setup
topic = Topic.first
assert_raises(ActiveModel::MissingAttributeError) { topic.update_columns(no_column_exists: "Hello!") }
assert_raises(ActiveModel::UnknownAttributeError) { topic.update(no_column_exists: "Hello!") }
assert_raises(ActiveModel::MissingAttributeError) { topic[:no_column_exists] = "Hello!" }
end

test "write_attribute does not raise when the attribute isn't selected" do
topic = Topic.select(:id).first
assert_nothing_raised { topic[:title] = "Hello!" }
end

test "write_attribute allows writing to aliased attributes" do
Expand Down Expand Up @@ -369,12 +375,11 @@ def setup
assert_equal "Don't change the topic", topic[:heading]
end

test "read_attribute raises ActiveModel::MissingAttributeError when the attribute does not exist" do
computer = Computer.select("id").first
test "read_attribute raises ActiveModel::MissingAttributeError when the attribute isn't selected" do
computer = Computer.select(:id, :extendedWarranty).first
assert_raises(ActiveModel::MissingAttributeError) { computer[:developer] }
assert_raises(ActiveModel::MissingAttributeError) { computer[:extendedWarranty] }
assert_raises(ActiveModel::MissingAttributeError) { computer[:no_column_exists] = "Hello!" }
assert_nothing_raised { computer[:developer] = "Hello!" }
assert_nothing_raised { computer[:extendedWarranty] }
assert_nothing_raised { computer[:no_column_exists] }
end

test "read_attribute when false" do
Expand Down

0 comments on commit 556025d

Please sign in to comment.