Tidy up the tests

* Use assert_nil where appropriate
* Lead with the expected value in collection_serializer_test.rb, etc
 so that expected/actual in test failure messages are not reversed
This commit is contained in:
Ben Woosley 2016-01-07 11:19:14 -08:00
parent 7d4f0c5c8a
commit 0a6c133d25
4 changed files with 15 additions and 15 deletions

View File

@ -102,7 +102,7 @@ module ActiveModel::Serializer::Lint
def test_updated_at def test_updated_at
assert_respond_to resource, :updated_at assert_respond_to resource, :updated_at
actual_arity = resource.method(:updated_at).arity actual_arity = resource.method(:updated_at).arity
assert_equal actual_arity, 0, "expected #{actual_arity.inspect} to be 0" assert_equal 0, actual_arity
end end
# Passes if the object responds to <tt>id</tt> and if it takes no # Passes if the object responds to <tt>id</tt> and if it takes no
@ -113,7 +113,7 @@ module ActiveModel::Serializer::Lint
# It is not required unless caching is enabled. # It is not required unless caching is enabled.
def test_id def test_id
assert_respond_to resource, :id assert_respond_to resource, :id
assert_equal resource.method(:id).arity, 0 assert_equal 0, resource.method(:id).arity
end end
# Passes if the object's class responds to <tt>model_name</tt> and if it # Passes if the object's class responds to <tt>model_name</tt> and if it

View File

@ -28,7 +28,7 @@ module ActiveModel
comment = Comment.new comment = Comment.new
post = Post.new post = Post.new
serializer = ArraySerializer.new([comment, post]) serializer = ArraySerializer.new([comment, post])
assert_equal serializer.json_key, 'comments' assert_equal 'comments', serializer.json_key
end) end)
assert_match(/Calling deprecated ArraySerializer/, stderr) assert_match(/Calling deprecated ArraySerializer/, stderr)
end end

View File

@ -36,7 +36,7 @@ module ActiveModel
assert_kind_of PostSerializer, serializers.last assert_kind_of PostSerializer, serializers.last
assert_kind_of Post, serializers.last.object assert_kind_of Post, serializers.last.object
assert_equal serializers.last.custom_options[:some], :options assert_equal :options, serializers.last.custom_options[:some]
end end
def test_serializer_option_not_passed_to_each_serializer def test_serializer_option_not_passed_to_each_serializer
@ -47,50 +47,50 @@ module ActiveModel
def test_root_default def test_root_default
@serializer = collection_serializer.new([@comment, @post]) @serializer = collection_serializer.new([@comment, @post])
assert_equal @serializer.root, nil assert_nil @serializer.root
end end
def test_root def test_root
expected = 'custom_root' expected = 'custom_root'
@serializer = collection_serializer.new([@comment, @post], root: expected) @serializer = collection_serializer.new([@comment, @post], root: expected)
assert_equal @serializer.root, expected assert_equal expected, @serializer.root
end end
def test_root_with_no_serializers def test_root_with_no_serializers
expected = 'custom_root' expected = 'custom_root'
@serializer = collection_serializer.new([], root: expected) @serializer = collection_serializer.new([], root: expected)
assert_equal @serializer.root, expected assert_equal expected, @serializer.root
end end
def test_json_key def test_json_key
assert_equal @serializer.json_key, 'comments' assert_equal 'comments', @serializer.json_key
end end
def test_json_key_with_resource_with_name_and_no_serializers def test_json_key_with_resource_with_name_and_no_serializers
serializer = collection_serializer.new(build_named_collection) serializer = collection_serializer.new(build_named_collection)
assert_equal serializer.json_key, 'me_resources' assert_equal 'me_resources', serializer.json_key
end end
def test_json_key_with_resource_with_nil_name_and_no_serializers def test_json_key_with_resource_with_nil_name_and_no_serializers
resource = [] resource = []
resource.define_singleton_method(:name) { nil } resource.define_singleton_method(:name) { nil }
serializer = collection_serializer.new(resource) serializer = collection_serializer.new(resource)
assert_equal serializer.json_key, nil assert_nil serializer.json_key
end end
def test_json_key_with_resource_without_name_and_no_serializers def test_json_key_with_resource_without_name_and_no_serializers
serializer = collection_serializer.new([]) serializer = collection_serializer.new([])
assert_equal serializer.json_key, nil assert_nil serializer.json_key
end end
def test_json_key_with_root def test_json_key_with_root
serializer = collection_serializer.new(@resource, root: 'custom_root') serializer = collection_serializer.new(@resource, root: 'custom_root')
assert_equal serializer.json_key, 'custom_roots' assert_equal 'custom_roots', serializer.json_key
end end
def test_json_key_with_root_and_no_serializers def test_json_key_with_root_and_no_serializers
serializer = collection_serializer.new(build_named_collection, root: 'custom_root') serializer = collection_serializer.new(build_named_collection, root: 'custom_root')
assert_equal serializer.json_key, 'custom_roots' assert_equal 'custom_roots', serializer.json_key
end end
end end
end end

View File

@ -52,8 +52,8 @@ module ActiveModel
serializer = association.serializer serializer = association.serializer
options = association.options options = association.options
assert_equal key, :tags assert_equal :tags, key
assert_equal serializer, nil assert_nil serializer
assert_equal [{ name: '#hashtagged' }].to_json, options[:virtual_value].to_json assert_equal [{ name: '#hashtagged' }].to_json, options[:virtual_value].to_json
end end
end end