module ActiveModel::Serializer::Lint::Tests
def assert_instance_of(result, name)
def assert_instance_of(result, name) assert result.instance_of?(name), "#{result} should be an instance of #{name}" end
def resource
def resource @resource or fail "'@resource' must be set as the linted object" end
def test_active_model_errors
def test_active_model_errors assert_respond_to resource, :errors end
def test_active_model_errors_human_attribute_name
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
def test_active_model_errors_lookup_ancestors
def test_active_model_errors_lookup_ancestors assert_respond_to resource.class, :lookup_ancestors assert_equal 0, resource.class.method(:lookup_ancestors).arity end
def test_as_json
which includes ActiveModel::Serializers::JSON.
Typically, it is implemented either by including ActiveModel::Serialization
It may delegate to serializable_hash
as_json returns a hash representation of a serialized object.
Fails otherwise.
zero or one arguments.
Passes if the object responds to as_json and if it takes
def test_as_json assert_respond_to resource, :as_json resource.as_json resource.as_json(nil) end
def test_cache_key
and is part of the (self-expiring) cache_key, which is used by the
cache_key returns a (self-expiring) unique key for the object,
Fails otherwise.
Passes if the object responds to cache_key
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
def test_id
id returns a unique identifier for the object.
Fails otherwise.
arguments.
Passes if the object responds to id and if it takes no
def test_id assert_respond_to resource, :id assert_equal 0, resource.method(:id).arity end
def test_model_name
It is used by the serializer to identify the object's type.
model_name returns an ActiveModel::Name instance.
Fails otherwise.
is in an instance of +ActiveModel::Name+.
Passes if the object's class responds to model_name and if it
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
def test_read_attribute_for_serialization
read_attribute_for_serialization gets the attribute value for serialization
Fails otherwise.
and if it requires one argument (the attribute to be read).
Passes if the object responds to read_attribute_for_serialization
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
def test_serializable_hash
serializable_hash returns a hash representation of a object's attributes.
Fails otherwise.
zero or one arguments.
Passes if the object responds to serializable_hash and if it takes
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
def test_to_json
It may be called on the result of as_json.
to_json returns a string representation (JSON) of a serialized object.
Fails otherwise.
zero or one arguments.
Passes if the object responds to to_json and if it takes
def test_to_json assert_respond_to resource, :to_json resource.to_json resource.to_json(nil) end
def test_updated_at
is part of the (self-expiring) cache_key, which is used by the adapter.
updated_at returns a Time object or iso8601 string and
Fails otherwise.
arguments.
Passes if the object responds to updated_at and if it takes no
def test_updated_at assert_respond_to resource, :updated_at actual_arity = resource.method(:updated_at).arity assert_equal 0, actual_arity end