active_model_serializers/test/serializable_resource_test.rb
Noah Silas 94db09b3f6 Fix RuboCop 0.40 linter errors (#1722)
These errors are breaking the build, which seems to use RuboCop 0.40 [1]
despite the Gemfile.lock pinning rubocop to 0.38.

New lints that I am updating the code style to reflect:

- Style/EmptyCaseCondition: Do not use empty case condition, instead use
  an if expression.

- Style/MultilineArrayBraceLayout: Closing array brace must be on the
  same line as the last array element when opening brace is on the same
  line as the first array element.

- Style/MultilineHashBraceLayout: Closing hash brace must be on the same
  line as the last hash element when opening brace is on the same line
  as the first hash element.

- Style/MultilineMethodCallBraceLayout: Closing method call brace must
  be on the line after the last argument when opening brace is on a
  separate line from the first argument.

[1] https://github.com/bbatsov/rubocop/releases/tag/v0.40.0
2016-05-26 12:58:05 -04:00

84 lines
3.0 KiB
Ruby

require 'test_helper'
module ActiveModelSerializers
class SerializableResourceTest < ActiveSupport::TestCase
def setup
@resource = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
@serializer = ProfileSerializer.new(@resource)
@adapter = ActiveModelSerializers::Adapter.create(@serializer)
@serializable_resource = SerializableResource.new(@resource)
end
def test_deprecation
assert_output(nil, /deprecated/) do
deprecated_serializable_resource = ActiveModel::SerializableResource.new(@resource)
assert_equal(@serializable_resource.as_json, deprecated_serializable_resource.as_json)
end
end
def test_serializable_resource_delegates_serializable_hash_to_the_adapter
options = nil
assert_equal @adapter.serializable_hash(options), @serializable_resource.serializable_hash(options)
end
def test_serializable_resource_delegates_to_json_to_the_adapter
options = nil
assert_equal @adapter.to_json(options), @serializable_resource.to_json(options)
end
def test_serializable_resource_delegates_as_json_to_the_adapter
options = nil
assert_equal @adapter.as_json(options), @serializable_resource.as_json(options)
end
def test_use_adapter_with_adapter_option
assert SerializableResource.new(@resource, { adapter: 'json' }).use_adapter?
end
def test_use_adapter_with_adapter_option_as_false
refute SerializableResource.new(@resource, { adapter: false }).use_adapter?
end
class SerializableResourceErrorsTest < Minitest::Test
def test_serializable_resource_with_errors
options = nil
resource = ModelWithErrors.new
resource.errors.add(:name, 'must be awesome')
serializable_resource = ActiveModelSerializers::SerializableResource.new(
resource, {
serializer: ActiveModel::Serializer::ErrorSerializer,
adapter: :json_api
}
)
expected_response_document = {
:errors => [
{ :source => { :pointer => '/data/attributes/name' }, :detail => 'must be awesome' }
]
}
assert_equal serializable_resource.as_json(options), expected_response_document
end
def test_serializable_resource_with_collection_containing_errors
options = nil
resources = []
resources << resource = ModelWithErrors.new
resource.errors.add(:title, 'must be amazing')
resources << ModelWithErrors.new
serializable_resource = SerializableResource.new(
resources, {
serializer: ActiveModel::Serializer::ErrorsSerializer,
each_serializer: ActiveModel::Serializer::ErrorSerializer,
adapter: :json_api
}
)
expected_response_document = {
:errors => [
{ :source => { :pointer => '/data/attributes/title' }, :detail => 'must be amazing' }
]
}
assert_equal serializable_resource.as_json(options), expected_response_document
end
end
end
end