Skip to content

Commit 1dc2b74

Browse files
authored
Merge pull request #1902 from bf4/fix_intermittent_relationship_test_failures
Fix intermittent test failures by not mutating global state
2 parents d34069b + 1d4cd6f commit 1dc2b74

File tree

1 file changed

+39
-18
lines changed

1 file changed

+39
-18
lines changed

test/adapter/json_api/relationships_test.rb

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,29 @@ def test_relationship_simple_link
103103
self: '//example.com/link_author/relationships/bio'
104104
}
105105
}
106-
assert_relationship(:bio, expected)
106+
107+
author = @author.dup
108+
assert_author_relationship_serialized(expected, author, :bio)
107109
end
108110

109111
def test_relationship_block_link
110112
expected = {
111113
data: { id: '1337', type: 'profiles' },
112114
links: { related: '//example.com/profiles/1337' }
113115
}
114-
assert_relationship(:profile, expected)
116+
117+
author = @author.dup
118+
assert_author_relationship_serialized(expected, author, :profile)
115119
end
116120

117121
def test_relationship_nil_link
118-
@author.profile.id = 123
119122
expected = {
120123
data: { id: '123', type: 'profiles' }
121124
}
122-
assert_relationship(:profile, expected)
125+
126+
author = @author.dup
127+
author.profile.id = 123
128+
assert_author_relationship_serialized(expected, author, :profile)
123129
end
124130

125131
def test_relationship_block_link_href
@@ -129,7 +135,9 @@ def test_relationship_block_link_href
129135
related: { href: '//example.com/locations/1337' }
130136
}
131137
}
132-
assert_relationship(:locations, expected)
138+
139+
author = @author.dup
140+
assert_author_relationship_serialized(expected, author, :locations)
133141
end
134142

135143
def test_relationship_block_link_href_and_meta
@@ -142,7 +150,9 @@ def test_relationship_block_link_href_and_meta
142150
}
143151
}
144152
}
145-
assert_relationship(:posts, expected)
153+
154+
author = @author.dup
155+
assert_author_relationship_serialized(expected, author, :posts)
146156
end
147157

148158
def test_relationship_block_link_meta
@@ -154,27 +164,33 @@ def test_relationship_block_link_meta
154164
}
155165
}
156166
}
157-
assert_relationship(:comments, expected)
167+
168+
author = @author.dup
169+
assert_author_relationship_serialized(expected, author, :comments)
158170
end
159171

160172
def test_relationship_meta
161173
expected = {
162174
data: [{ id: 'from-serializer-method', type: 'roles' }],
163175
meta: { count: 1 }
164176
}
165-
assert_relationship(:roles, expected)
177+
178+
author = @author.dup
179+
assert_author_relationship_serialized(expected, author, :roles)
166180
end
167181

168182
def test_relationship_not_including_data
169-
@author.define_singleton_method(:read_attribute_for_serialization) do |attr|
170-
fail 'should not be called' if attr == :blog
171-
super(attr)
172-
end
173183
expected = {
174184
links: { self: '//example.com/link_author/relationships/blog' }
175185
}
186+
187+
author = @author.dup
188+
author.define_singleton_method(:read_attribute_for_serialization) do |attr|
189+
fail 'should not be called' if attr == :blog
190+
super(attr)
191+
end
176192
assert_nothing_raised do
177-
assert_relationship(:blog, expected)
193+
assert_author_relationship_serialized(expected, author, :blog)
178194
end
179195
end
180196

@@ -183,7 +199,9 @@ def test_relationship_including_data_explicit
183199
data: { id: '1337', type: 'authors' },
184200
meta: { name: 'Dan Brown' }
185201
}
186-
assert_relationship(:reviewer, expected)
202+
203+
author = @author.dup
204+
assert_author_relationship_serialized(expected, author, :reviewer)
187205
end
188206

189207
def test_relationship_with_everything
@@ -197,14 +215,17 @@ def test_relationship_with_everything
197215
},
198216
meta: { liked: true }
199217
}
200-
assert_relationship(:likes, expected)
218+
219+
author = @author.dup
220+
assert_author_relationship_serialized(expected, author, :likes)
201221
end
202222

203223
private
204224

205-
def assert_relationship(relationship_name, expected)
206-
hash = serializable(@author, adapter: :json_api).serializable_hash
207-
assert_equal(expected, hash[:data][:relationships][relationship_name])
225+
def assert_author_relationship_serialized(expected, author, relationship_name)
226+
hash = serializable(author, adapter: :json_api).serializable_hash
227+
actual_relationship = hash[:data][:relationships][relationship_name]
228+
assert_equal(expected, actual_relationship)
208229
end
209230
end
210231
end

0 commit comments

Comments
 (0)