Skip to content

Commit

Permalink
Tidy up the tests
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Empact committed Jan 7, 2016
1 parent 7d4f0c5 commit 0a6c133
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions lib/active_model/serializer/lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_cache_key
def test_updated_at
assert_respond_to resource, :updated_at
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

# Passes if the object responds to <tt>id</tt> and if it takes no
Expand All @@ -113,7 +113,7 @@ def test_updated_at
# It is not required unless caching is enabled.
def test_id
assert_respond_to resource, :id
assert_equal resource.method(:id).arity, 0
assert_equal 0, resource.method(:id).arity
end

# Passes if the object's class responds to <tt>model_name</tt> and if it
Expand Down
2 changes: 1 addition & 1 deletion test/array_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_json_key_with_root_warns_when_using_array_serializer
comment = Comment.new
post = Post.new
serializer = ArraySerializer.new([comment, post])
assert_equal serializer.json_key, 'comments'
assert_equal 'comments', serializer.json_key
end)
assert_match(/Calling deprecated ArraySerializer/, stderr)
end
Expand Down
20 changes: 10 additions & 10 deletions test/collection_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_each_object_should_be_serialized_with_appropriate_serializer
assert_kind_of PostSerializer, serializers.last
assert_kind_of Post, serializers.last.object

assert_equal serializers.last.custom_options[:some], :options
assert_equal :options, serializers.last.custom_options[:some]
end

def test_serializer_option_not_passed_to_each_serializer
Expand All @@ -47,50 +47,50 @@ def test_serializer_option_not_passed_to_each_serializer

def test_root_default
@serializer = collection_serializer.new([@comment, @post])
assert_equal @serializer.root, nil
assert_nil @serializer.root
end

def test_root
expected = 'custom_root'
@serializer = collection_serializer.new([@comment, @post], root: expected)
assert_equal @serializer.root, expected
assert_equal expected, @serializer.root
end

def test_root_with_no_serializers
expected = 'custom_root'
@serializer = collection_serializer.new([], root: expected)
assert_equal @serializer.root, expected
assert_equal expected, @serializer.root
end

def test_json_key
assert_equal @serializer.json_key, 'comments'
assert_equal 'comments', @serializer.json_key
end

def test_json_key_with_resource_with_name_and_no_serializers
serializer = collection_serializer.new(build_named_collection)
assert_equal serializer.json_key, 'me_resources'
assert_equal 'me_resources', serializer.json_key
end

def test_json_key_with_resource_with_nil_name_and_no_serializers
resource = []
resource.define_singleton_method(:name) { nil }
serializer = collection_serializer.new(resource)
assert_equal serializer.json_key, nil
assert_nil serializer.json_key
end

def test_json_key_with_resource_without_name_and_no_serializers
serializer = collection_serializer.new([])
assert_equal serializer.json_key, nil
assert_nil serializer.json_key
end

def test_json_key_with_root
serializer = collection_serializer.new(@resource, root: 'custom_root')
assert_equal serializer.json_key, 'custom_roots'
assert_equal 'custom_roots', serializer.json_key
end

def test_json_key_with_root_and_no_serializers
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
Expand Down
4 changes: 2 additions & 2 deletions test/serializers/associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def test_has_many_with_no_serializer
serializer = association.serializer
options = association.options

assert_equal key, :tags
assert_equal serializer, nil
assert_equal :tags, key
assert_nil serializer
assert_equal [{ name: '#hashtagged' }].to_json, options[:virtual_value].to_json
end
end
Expand Down

0 comments on commit 0a6c133

Please sign in to comment.