moduleActiveModel::Serializer::Lint# == Active \Model \Serializer \Lint \Tests## You can test whether an object is compliant with the Active \Model \Serializers# API by including <tt>ActiveModel::Serializer::Lint::Tests</tt> in your TestCase.# It will include tests that tell you whether your object is fully compliant,# or if not, which aspects of the API are not implemented.## Note an object is not required to implement all APIs in order to work# with Active \Model \Serializers. This module only intends to provide guidance in case# you want all features out of the box.## These tests do not attempt to determine the semantic correctness of the# returned values. For instance, you could implement <tt>serializable_hash</tt> to# always return +{}+, and the tests would pass. It is up to you to ensure# that the values are semantically meaningful.moduleTests# Passes if the object responds to <tt>serializable_hash</tt> and if it takes# zero or one arguments.# Fails otherwise.## <tt>serializable_hash</tt> returns a hash representation of a object's attributes.# Typically, it is implemented by including ActiveModel::Serialization.deftest_serializable_hashassert_respond_toresource,:serializable_hash,'The resource should respond to serializable_hash'resource.serializable_hashresource.serializable_hash(nil)end# Passes if the object responds to <tt>read_attribute_for_serialization</tt># and if it requires one argument (the attribute to be read).# Fails otherwise.## <tt>read_attribute_for_serialization</tt> gets the attribute value for serialization# Typically, it is implemented by including ActiveModel::Serialization.deftest_read_attribute_for_serializationassert_respond_toresource,:read_attribute_for_serialization,'The resource should respond to read_attribute_for_serialization'actual_arity=resource.method(:read_attribute_for_serialization).arityifdefined?(::Rubinius)# 1 for def read_attribute_for_serialization(name); end# -2 for alias :read_attribute_for_serialization :send for rbx because :shrug:assert_includes[1,-2],actual_arity,"expected #{actual_arity.inspect} to be 1 or -2"else# using absolute value since arity is:# 1 for def read_attribute_for_serialization(name); end# -1 for alias :read_attribute_for_serialization :sendassert_includes[1,-1],actual_arity,"expected #{actual_arity.inspect} to be 1 or -1"endend# Passes if the object responds to <tt>as_json</tt> and if it takes# zero or one arguments.# Fails otherwise.## <tt>as_json</tt> returns a hash representation of a serialized object.# It may delegate to <tt>serializable_hash</tt># Typically, it is implemented either by including ActiveModel::Serialization# which includes ActiveModel::Serializers::JSON.# or by the JSON gem when required.deftest_as_jsonassert_respond_toresource,:as_jsonresource.as_jsonresource.as_json(nil)end# Passes if the object responds to <tt>to_json</tt> and if it takes# zero or one arguments.# Fails otherwise.## <tt>to_json</tt> returns a string representation (JSON) of a serialized object.# It may be called on the result of <tt>as_json</tt>.# Typically, it is implemented on all objects when the JSON gem is required.deftest_to_jsonassert_respond_toresource,:to_jsonresource.to_jsonresource.to_json(nil)end# Passes if the object responds to <tt>cache_key</tt> and if it takes no# arguments (Rails 4.0) or a splat (Rails 4.1+).# Fails otherwise.## <tt>cache_key</tt> returns a (self-expiring) unique key for the object, and# is part of the (self-expiring) cache_key, which is used by the adapter.# It is not required unless caching is enabled.deftest_cache_keyassert_respond_toresource,:cache_keyactual_arity=resource.method(:cache_key).arity# using absolute value since arity is:# 0 for Rails 4.1+, *timestamp_names# -1 for Rails 4.0, no argumentsassert_includes[-1,0],actual_arity,"expected #{actual_arity.inspect} to be 0 or -1"end# Passes if the object responds to <tt>updated_at</tt> and if it takes no# arguments.# Fails otherwise.## <tt>updated_at</tt> returns a Time object or iso8601 string and# is part of the (self-expiring) cache_key, which is used by the adapter.# It is not required unless caching is enabled.deftest_updated_atassert_respond_toresource,:updated_atactual_arity=resource.method(:updated_at).arityassert_equal0,actual_arityend# Passes if the object responds to <tt>id</tt> and if it takes no# arguments.# Fails otherwise.## <tt>id</tt> returns a unique identifier for the object.# It is not required unless caching is enabled.deftest_idassert_respond_toresource,:idassert_equal0,resource.method(:id).arityend# Passes if the object's class responds to <tt>model_name</tt> and if it# is in an instance of +ActiveModel::Name+.# Fails otherwise.## <tt>model_name</tt> returns an ActiveModel::Name instance.# It is used by the serializer to identify the object's type.# It is not required unless caching is enabled.deftest_model_nameresource_class=resource.classassert_respond_toresource_class,:model_nameassert_instance_ofresource_class.model_name,ActiveModel::Nameenddeftest_active_model_errorsassert_respond_toresource,:errorsenddeftest_active_model_errors_human_attribute_nameassert_respond_toresource.class,:human_attribute_nameassert_equal(-2,resource.class.method(:human_attribute_name).arity)enddeftest_active_model_errors_lookup_ancestorsassert_respond_toresource.class,:lookup_ancestorsassert_equal0,resource.class.method(:lookup_ancestors).arityendprivatedefresource@resourceorfail"'@resource' must be set as the linted object"enddefassert_instance_of(result,name)assertresult.instance_of?(name),"#{result} should be an instance of #{name}"endendend