mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
58fc63fd96
The test was skipped because of an issue that, in the meantime, has been fixed: https://github.com/rubinius/rubinius/issues/3328. Using the latest Rubinius (the one currently on Travis CI), this is the result: ```sh $ ruby --version rubinius 2.5.3 (2.1.0 2482b093 2015-05-10 3.5.1 JI) [x86_64-darwin14.3.0] ``` **Before this PR** ```sh $ ruby -Itest test/cases/attribute_assignment_test.rb Run options: --seed 58569 .....S... Finished in 0.048278s, 186.4203 runs/s, 269.2738 assertions/s. 9 runs, 13 assertions, 0 failures, 0 errors, 1 skips You have skipped tests. Run with --verbose for details. ``` **After this PR** $ ruby -Itest test/cases/attribute_assignment_test.rb Run options: --seed 35720 ......... Finished in 0.029441s, 305.6961 runs/s, 475.5273 assertions/s. 9 runs, 14 assertions, 0 failures, 0 errors, 0 skips ```
107 lines
2.6 KiB
Ruby
107 lines
2.6 KiB
Ruby
require "cases/helper"
|
|
require "active_support/hash_with_indifferent_access"
|
|
|
|
class AttributeAssignmentTest < ActiveModel::TestCase
|
|
class Model
|
|
include ActiveModel::AttributeAssignment
|
|
|
|
attr_accessor :name, :description
|
|
|
|
def initialize(attributes = {})
|
|
assign_attributes(attributes)
|
|
end
|
|
|
|
def broken_attribute=(value)
|
|
raise ErrorFromAttributeWriter
|
|
end
|
|
|
|
protected
|
|
|
|
attr_writer :metadata
|
|
end
|
|
|
|
class ErrorFromAttributeWriter < StandardError
|
|
end
|
|
|
|
class ProtectedParams < ActiveSupport::HashWithIndifferentAccess
|
|
def permit!
|
|
@permitted = true
|
|
end
|
|
|
|
def permitted?
|
|
@permitted ||= false
|
|
end
|
|
|
|
def dup
|
|
super.tap do |duplicate|
|
|
duplicate.instance_variable_set :@permitted, permitted?
|
|
end
|
|
end
|
|
end
|
|
|
|
test "simple assignment" do
|
|
model = Model.new
|
|
|
|
model.assign_attributes(name: "hello", description: "world")
|
|
assert_equal "hello", model.name
|
|
assert_equal "world", model.description
|
|
end
|
|
|
|
test "assign non-existing attribute" do
|
|
model = Model.new
|
|
error = assert_raises(ActiveModel::UnknownAttributeError) do
|
|
model.assign_attributes(hz: 1)
|
|
end
|
|
|
|
assert_equal model, error.record
|
|
assert_equal "hz", error.attribute
|
|
end
|
|
|
|
test "assign private attribute" do
|
|
model = Model.new
|
|
assert_raises(ActiveModel::UnknownAttributeError) do
|
|
model.assign_attributes(metadata: { a: 1 })
|
|
end
|
|
end
|
|
|
|
test "does not swallow errors raised in an attribute writer" do
|
|
assert_raises(ErrorFromAttributeWriter) do
|
|
Model.new(broken_attribute: 1)
|
|
end
|
|
end
|
|
|
|
test "an ArgumentError is raised if a non-hash-like obejct is passed" do
|
|
assert_raises(ArgumentError) do
|
|
Model.new(1)
|
|
end
|
|
end
|
|
|
|
test "forbidden attributes cannot be used for mass assignment" do
|
|
params = ProtectedParams.new(name: "Guille", description: "m")
|
|
|
|
assert_raises(ActiveModel::ForbiddenAttributesError) do
|
|
Model.new(params)
|
|
end
|
|
end
|
|
|
|
test "permitted attributes can be used for mass assignment" do
|
|
params = ProtectedParams.new(name: "Guille", description: "desc")
|
|
params.permit!
|
|
model = Model.new(params)
|
|
|
|
assert_equal "Guille", model.name
|
|
assert_equal "desc", model.description
|
|
end
|
|
|
|
test "regular hash should still be used for mass assignment" do
|
|
model = Model.new(name: "Guille", description: "m")
|
|
|
|
assert_equal "Guille", model.name
|
|
assert_equal "m", model.description
|
|
end
|
|
|
|
test "assigning no attributes should not raise, even if the hash is un-permitted" do
|
|
model = Model.new
|
|
assert_nil model.assign_attributes(ProtectedParams.new({}))
|
|
end
|
|
end
|