active_model_serializers/test/unit/active_model/serializer/context_test.rb
Adrian Mugnolo and Santiago Pastorino 0d8ef2b165 Nest scope under context option
2014-01-06 15:50:30 -02:00

47 lines
1.4 KiB
Ruby

require 'test_helper'
module ActiveModel
class Serializer
class ContextTest < ActiveModel::TestCase
def test_context_using_a_hash
serializer = UserSerializer.new(nil, context: { a: 1, b: 2 })
assert_equal(1, serializer.context[:a])
assert_equal(2, serializer.context[:b])
end
def test_context_using_an_object
serializer = UserSerializer.new(nil, context: Struct.new(:a, :b).new(1, 2))
assert_equal(1, serializer.context.a)
assert_equal(2, serializer.context.b)
end
end
class ContextAssociationTest < ActiveModel::TestCase
def setup
@association = UserSerializer._associations[:profile]
@old_association = @association.dup
@user = User.new({ name: 'Name 1', email: 'mail@server.com', gender: 'M' })
@user_serializer = UserSerializer.new(@user, context: { admin: true })
end
def teardown
UserSerializer._associations[:profile] = @old_association
end
def test_context_passed_through
@association.serializer_class = Class.new(ActiveModel::Serializer) do
def name
context[:admin] ? 'Admin' : 'User'
end
attributes :name
end
assert_equal({
name: 'Name 1', email: 'mail@server.com', profile: { name: 'Admin' }
}, @user_serializer.serializable_hash)
end
end
end
end