Module: ActiveModel::Serializer::Lint::Tests
- Defined in:
- lib/active_model/serializer/lint.rb
Overview
Active Model Serializer Lint Tests
You can test whether an object is compliant with the Active Model Serializers API by including ActiveModel::Serializer::Lint::Tests
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 serializable_hash
to always return {}, and the tests would pass. It is up to you to ensure that the values are semantically meaningful.
Instance Method Summary collapse
- #test_active_model_errors ⇒ Object
- #test_active_model_errors_human_attribute_name ⇒ Object
- #test_active_model_errors_lookup_ancestors ⇒ Object
-
#test_as_json ⇒ Object
Passes if the object responds to
as_json
and if it takes zero or one arguments. -
#test_cache_key ⇒ Object
Passes if the object responds to
cache_key
Fails otherwise. -
#test_id ⇒ Object
Passes if the object responds to
id
and if it takes no arguments. -
#test_model_name ⇒ Object
Passes if the object’s class responds to
model_name
and if it is in an instance ofActiveModel::Name
. -
#test_read_attribute_for_serialization ⇒ Object
Passes if the object responds to
read_attribute_for_serialization
and if it requires one argument (the attribute to be read). -
#test_serializable_hash ⇒ Object
Passes if the object responds to
serializable_hash
and if it takes zero or one arguments. -
#test_to_json ⇒ Object
Passes if the object responds to
to_json
and if it takes zero or one arguments. -
#test_updated_at ⇒ Object
Passes if the object responds to
updated_at
and if it takes no arguments.
Instance Method Details
#test_active_model_errors ⇒ Object
124 125 126 |
# File 'lib/active_model/serializer/lint.rb', line 124 def test_active_model_errors assert_respond_to resource, :errors end |
#test_active_model_errors_human_attribute_name ⇒ Object
128 129 130 131 |
# File 'lib/active_model/serializer/lint.rb', line 128 def test_active_model_errors_human_attribute_name assert_respond_to resource.class, :human_attribute_name assert_equal(-2, resource.class.method(:human_attribute_name).arity) end |
#test_active_model_errors_lookup_ancestors ⇒ Object
133 134 135 136 |
# File 'lib/active_model/serializer/lint.rb', line 133 def test_active_model_errors_lookup_ancestors assert_respond_to resource.class, :lookup_ancestors assert_equal 0, resource.class.method(:lookup_ancestors).arity end |
#test_as_json ⇒ Object
Passes if the object responds to as_json
and if it takes zero or one arguments. Fails otherwise.
as_json
returns a hash representation of a serialized object. It may delegate to serializable_hash
Typically, it is implemented either by including ActiveModel::Serialization which includes ActiveModel::Serializers::JSON. or by the JSON gem when required.
56 57 58 59 60 |
# File 'lib/active_model/serializer/lint.rb', line 56 def test_as_json assert_respond_to resource, :as_json resource.as_json resource.as_json(nil) end |
#test_cache_key ⇒ Object
Passes if the object responds to cache_key
Fails otherwise.
cache_key
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.
81 82 83 84 85 |
# File 'lib/active_model/serializer/lint.rb', line 81 def test_cache_key assert_respond_to resource, :cache_key actual_arity = resource.method(:cache_key).arity assert_includes [-1, 0], actual_arity, "expected #{actual_arity.inspect} to be 0 or -1" end |
#test_id ⇒ Object
Passes if the object responds to id
and if it takes no arguments. Fails otherwise.
id
returns a unique identifier for the object. It is not required unless caching is enabled.
106 107 108 109 |
# File 'lib/active_model/serializer/lint.rb', line 106 def test_id assert_respond_to resource, :id assert_equal 0, resource.method(:id).arity end |
#test_model_name ⇒ Object
Passes if the object’s class responds to model_name
and if it is in an instance of ActiveModel::Name
. Fails otherwise.
model_name
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.
118 119 120 121 122 |
# File 'lib/active_model/serializer/lint.rb', line 118 def test_model_name resource_class = resource.class assert_respond_to resource_class, :model_name assert_instance_of resource_class.model_name, ActiveModel::Name end |
#test_read_attribute_for_serialization ⇒ Object
Passes if the object responds to read_attribute_for_serialization
and if it requires one argument (the attribute to be read). Fails otherwise.
read_attribute_for_serialization
gets the attribute value for serialization Typically, it is implemented by including ActiveModel::Serialization.
38 39 40 41 42 43 44 45 |
# File 'lib/active_model/serializer/lint.rb', line 38 def test_read_attribute_for_serialization assert_respond_to resource, :read_attribute_for_serialization, 'The resource should respond to read_attribute_for_serialization' actual_arity = resource.method(:read_attribute_for_serialization).arity # using absolute value since arity is: # 1 for def read_attribute_for_serialization(name); end # -1 for alias :read_attribute_for_serialization :send assert_equal 1, actual_arity.abs, "expected #{actual_arity.inspect}.abs to be 1 or -1" end |
#test_serializable_hash ⇒ Object
Passes if the object responds to serializable_hash
and if it takes zero or one arguments. Fails otherwise.
serializable_hash
returns a hash representation of a object’s attributes. Typically, it is implemented by including ActiveModel::Serialization.
26 27 28 29 30 |
# File 'lib/active_model/serializer/lint.rb', line 26 def test_serializable_hash assert_respond_to resource, :serializable_hash, 'The resource should respond to serializable_hash' resource.serializable_hash resource.serializable_hash(nil) end |
#test_to_json ⇒ Object
Passes if the object responds to to_json
and if it takes zero or one arguments. Fails otherwise.
to_json
returns a string representation (JSON) of a serialized object. It may be called on the result of as_json
. Typically, it is implemented on all objects when the JSON gem is required.
69 70 71 72 73 |
# File 'lib/active_model/serializer/lint.rb', line 69 def test_to_json assert_respond_to resource, :to_json resource.to_json resource.to_json(nil) end |
#test_updated_at ⇒ Object
Passes if the object responds to updated_at
and if it takes no arguments. Fails otherwise.
updated_at
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.
94 95 96 97 98 |
# File 'lib/active_model/serializer/lint.rb', line 94 def test_updated_at assert_respond_to resource, :updated_at actual_arity = resource.method(:updated_at).arity assert_equal 0, actual_arity end |