Skip to content

Commit

Permalink
Add failing test for AMS::Model accessor vs. attributes mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
bf4 committed Sep 1, 2016
1 parent 1dc2b74 commit 206e71e
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion test/active_model_serializers/model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module ActiveModelSerializers
class ModelTest < ActiveSupport::TestCase
include ActiveModel::Serializer::Lint::Tests

def setup
setup do
@resource = ActiveModelSerializers::Model.new
end

Expand All @@ -18,5 +18,53 @@ def test_initialization_with_string_keys

assert_equal model_instance.read_attribute_for_serialization(:key), value
end

def test_attributes_can_be_read_for_serialization
klass = Class.new(ActiveModelSerializers::Model) do
attr_accessor :one, :two, :three
end
original_attributes = { one: 1, two: 2, three: 3 }
instance = klass.new(original_attributes)

# Initial value
expected_attributes = { one: 1, two: 2, three: 3 }
assert_equal expected_attributes, instance.attributes
assert_equal 1, instance.one
assert_equal 1, instance.read_attribute_for_serialization(:one)

# Change via accessor
instance.one = :not_one

expected_attributes = { one: :not_one, two: 2, three: 3 }
assert_equal expected_attributes, instance.attributes
assert_equal :not_one, instance.one
assert_equal :not_one, instance.read_attribute_for_serialization(:one)
end

def test_id_attribute_can_be_read_for_serialization
klass = Class.new(ActiveModelSerializers::Model) do
attr_accessor :id, :one, :two, :three
end
self.class.const_set(:SomeTestModel, klass)
original_attributes = { id: :ego, one: 1, two: 2, three: 3 }
instance = klass.new(original_attributes)

# Initial value
expected_attributes = { id: :ego, one: 1, two: 2, three: 3 }
assert_equal expected_attributes, instance.attributes
assert_equal 1, instance.one
assert_equal 1, instance.read_attribute_for_serialization(:one)

# Change via accessor
instance.id = :superego

expected_attributes = { id: :superego, one: 1, two: 2, three: 3 }
assert_equal expected_attributes, instance.attributes
assert_equal :superego, instance.id
assert_equal :superego, instance.read_attribute_for_serialization(:id)
ensure
self.class.send(:remove_const, :SomeTestModel)
end

end
end

0 comments on commit 206e71e

Please sign in to comment.