From 1d4cd6f1049432f8f2ab9ffad969a09daf6b59e9 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Wed, 31 Aug 2016 19:24:21 -0500 Subject: [PATCH] Fix intermittent test failures by not mutating global state e.g. https://ci.appveyor.com/project/bf4/active-model-serializers/build/415/job/qs8895cvla8fl0rf 1) Failure: ActiveModel::Serializer::Adapter::JsonApi::RelationshipTest#test_relationship_including_data_explicit [C:/projects/active-model-serializers/test/adapter/json_api/relationships_test.rb:186]: --- expected +++ actual @@ -1 +1 @@ -{:data=>{:id=>"1337", :type=>"authors"}, :meta=>{:name=>"Dan Brown"}} +{:data=>{:id=>"1337", :type=>"authors" https://travis-ci.org/rails-api/active_model_serializers/jobs/156678147 1) Failure: ActiveModel::Serializer::Adapter::JsonApi::RelationshipTest#test_relationship_with_everything [/home/travis/build/rails-api/active_model_serializers/test/adapter/json_api/relationships_test.rb:200]: --- expected +++ actual @@ -1 +1 @@ -{:data=>[{:id=>"1337", :type=>"likes"}], :links=>{:related=>{:href=>"//example.com/likes/1337", :meta=>{:ids=>"1337"}}}, :meta=>{:liked=>true}} +{:data=>[{:id=>"1337", :type=>"likes"}], :links=>{:related=>{:href=>"//example.com/likes/1337", :meta=>{:ids=>"1337"}}}} --- test/adapter/json_api/relationships_test.rb | 57 ++++++++++++++------- 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/test/adapter/json_api/relationships_test.rb b/test/adapter/json_api/relationships_test.rb index eaeac465..9f663943 100644 --- a/test/adapter/json_api/relationships_test.rb +++ b/test/adapter/json_api/relationships_test.rb @@ -103,7 +103,9 @@ module ActiveModel self: '//example.com/link_author/relationships/bio' } } - assert_relationship(:bio, expected) + + author = @author.dup + assert_author_relationship_serialized(expected, author, :bio) end def test_relationship_block_link @@ -111,15 +113,19 @@ module ActiveModel data: { id: '1337', type: 'profiles' }, links: { related: '//example.com/profiles/1337' } } - assert_relationship(:profile, expected) + + author = @author.dup + assert_author_relationship_serialized(expected, author, :profile) end def test_relationship_nil_link - @author.profile.id = 123 expected = { data: { id: '123', type: 'profiles' } } - assert_relationship(:profile, expected) + + author = @author.dup + author.profile.id = 123 + assert_author_relationship_serialized(expected, author, :profile) end def test_relationship_block_link_href @@ -129,7 +135,9 @@ module ActiveModel related: { href: '//example.com/locations/1337' } } } - assert_relationship(:locations, expected) + + author = @author.dup + assert_author_relationship_serialized(expected, author, :locations) end def test_relationship_block_link_href_and_meta @@ -142,7 +150,9 @@ module ActiveModel } } } - assert_relationship(:posts, expected) + + author = @author.dup + assert_author_relationship_serialized(expected, author, :posts) end def test_relationship_block_link_meta @@ -154,7 +164,9 @@ module ActiveModel } } } - assert_relationship(:comments, expected) + + author = @author.dup + assert_author_relationship_serialized(expected, author, :comments) end def test_relationship_meta @@ -162,19 +174,23 @@ module ActiveModel data: [{ id: 'from-serializer-method', type: 'roles' }], meta: { count: 1 } } - assert_relationship(:roles, expected) + + author = @author.dup + assert_author_relationship_serialized(expected, author, :roles) end def test_relationship_not_including_data - @author.define_singleton_method(:read_attribute_for_serialization) do |attr| - fail 'should not be called' if attr == :blog - super(attr) - end expected = { links: { self: '//example.com/link_author/relationships/blog' } } + + author = @author.dup + author.define_singleton_method(:read_attribute_for_serialization) do |attr| + fail 'should not be called' if attr == :blog + super(attr) + end assert_nothing_raised do - assert_relationship(:blog, expected) + assert_author_relationship_serialized(expected, author, :blog) end end @@ -183,7 +199,9 @@ module ActiveModel data: { id: '1337', type: 'authors' }, meta: { name: 'Dan Brown' } } - assert_relationship(:reviewer, expected) + + author = @author.dup + assert_author_relationship_serialized(expected, author, :reviewer) end def test_relationship_with_everything @@ -197,14 +215,17 @@ module ActiveModel }, meta: { liked: true } } - assert_relationship(:likes, expected) + + author = @author.dup + assert_author_relationship_serialized(expected, author, :likes) end private - def assert_relationship(relationship_name, expected) - hash = serializable(@author, adapter: :json_api).serializable_hash - assert_equal(expected, hash[:data][:relationships][relationship_name]) + def assert_author_relationship_serialized(expected, author, relationship_name) + hash = serializable(author, adapter: :json_api).serializable_hash + actual_relationship = hash[:data][:relationships][relationship_name] + assert_equal(expected, actual_relationship) end end end