mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Improve delegation test coverage
This commit is contained in:
parent
0d4e2d6ba4
commit
7a93c9528f
@ -1,5 +1,7 @@
|
|||||||
module AMS
|
module AMS
|
||||||
module Delegatable
|
module Delegatable
|
||||||
|
KERNEL_METHOD_METHOD = ::Kernel.instance_method(:method)
|
||||||
|
|
||||||
# delegate constant lookup to Object
|
# delegate constant lookup to Object
|
||||||
def const_missing(name)
|
def const_missing(name)
|
||||||
::Object.const_get(name)
|
::Object.const_get(name)
|
||||||
@ -12,6 +14,10 @@ module AMS
|
|||||||
__send__(*args)
|
__send__(*args)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def method(method_name)
|
||||||
|
AMS::Delegatable::KERNEL_METHOD_METHOD.bind(self).call(method_name)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def method_missing(name, *args, &block)
|
def method_missing(name, *args, &block)
|
||||||
@ -21,16 +27,6 @@ module AMS
|
|||||||
def respond_to_missing?(name, include_private = false)
|
def respond_to_missing?(name, include_private = false)
|
||||||
object.respond_to?(name, include_private)
|
object.respond_to?(name, include_private)
|
||||||
end
|
end
|
||||||
|
|
||||||
const_set(:KERNEL_METHOD_METHOD, ::Kernel.instance_method(:method))
|
|
||||||
def method_handle_for(method_name)
|
|
||||||
KERNEL_METHOD_METHOD.bind(self).call(method_name)
|
|
||||||
rescue NameError => original
|
|
||||||
handle = self.method(method_name)
|
|
||||||
raise original unless handle.is_a? Method
|
|
||||||
handle
|
|
||||||
end
|
|
||||||
alias method method_handle_for
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -5,6 +5,7 @@ module AMS
|
|||||||
class Serializer
|
class Serializer
|
||||||
class ObjectDelegationTest < Test
|
class ObjectDelegationTest < Test
|
||||||
class ParentModelSerializer < Serializer
|
class ParentModelSerializer < Serializer
|
||||||
|
attribute :some_method
|
||||||
end
|
end
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@ -26,6 +27,29 @@ module AMS
|
|||||||
def test_model_delegates_respond_to_object
|
def test_model_delegates_respond_to_object
|
||||||
refute @serializer_instance.respond_to?(:not_delegated?)
|
refute @serializer_instance.respond_to?(:not_delegated?)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_model_delegates_respond_to_missing_to_object
|
||||||
|
refute @serializer_instance.respond_to?(:not_delegated?)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_serializer_binds_method_method
|
||||||
|
file, = @serializer_instance.method(:some_method).source_location
|
||||||
|
assert_match(%r{/lib/ams/serializer.rb\z}, file)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_serializer_instance_raises_method_missing
|
||||||
|
exception = assert_raises(NameError) do
|
||||||
|
@serializer_instance.non_existent_method
|
||||||
|
end
|
||||||
|
assert_match(%r{undefined method `non_existent_method' for #<ParentModel:[^@]+@id=1>\z}, exception.message)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_serializer_object_raises_method_missing
|
||||||
|
exception = assert_raises(NameError) do
|
||||||
|
@serializer_instance.method(:non_existent_method)
|
||||||
|
end
|
||||||
|
assert_match(%r{undefined method `non_existent_method' for class `AMS::Serializer::ObjectDelegationTest::ParentModelSerializer'\z}, exception.message)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user