Skip to content

Commit 14439aa

Browse files
committed
Use model that doesn't fail with race condition
For some reason, the post would sometimes be serialized as "{\"id\":\"1\", + \"type\":\"posts\", \"attributes\":{\"title\":\"New Post\",\"body\":\"Body\"}, \"comments\":[{\"id\":1,\"body\":\"ZOMG A COMMENT\"}], \"blog\":{\"id\":999,\"name\":\"Custom blog\"}, \"author\":{\"id\":1,\"name\":\"Joao Moura.\"}}" instead of: "{\"id\":1, - \"title\":\"New Post\",\"body\":\"Body\", \"comments\":[{\"id\":1,\"body\":\"ZOMG A COMMENT\"}], \"blog\":{\"id\":999,\"name\":\"Custom blog\"},\ "author\":{\"id\":1,\"name\":\"Joao Moura.\"}}" To reproduce prior to this PR: SEED=55284 rake 1) Failure: ActionController::Serialization::ExplicitSerializerTest#test_render_using_explicit_each_serializer [active_model_serializers/test/action_controller/explicit_serializer_test.rb:139]: --- expected +++ actual @@ -1 +1 @@ -"{\"id\":1,\"title\":\"New Post\",\"body\":\"Body\",\"comments\":[{\"id\":1,\"body\":\"ZOMG A COMMENT\"}],\"blog\":{\"id\":999,\"name\":\"Custom blog\"},\"author\":{\"id\":1,\"name\":\"Joao Moura.\"}}" +"{\"id\":\"1\",\"type\":\"posts\",\"attributes\":{\"title\":\"New Post\",\"body\":\"Body\"},\"comments\":[{\"id\":1,\"body\":\"ZOMG A COMMENT\"}],\"blog\":{\"id\":999,\"name\":\"Custom blog\"},\"author\":{\"id\":1,\"name\":\"Joao Moura.\"}}" 137 runs, 211 assertions, 1 failures, 0 errors, 0 skips rake aborted! Command failed with status (1): [ruby -I"lib:test" -r./test/test_helper.rb "/$HOME/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/rake_test_loader.rb" "test/action_controller/adapter_selector_test.rb" "test/action_controller/explicit_serializer_test.rb" "test/action_controller/json_api_linked_test.rb" "test/action_controller/rescue_from_test.rb" "test/action_controller/serialization_scope_name_test.rb" "test/action_controller/serialization_test.rb" "test/adapter/fragment_cache_test.rb" "test/adapter/json/belongs_to_test.rb" "test/adapter/json/collection_test.rb" "test/adapter/json/has_many_test.rb" "test/adapter/json_api/belongs_to_test.rb" "test/adapter/json_api/collection_test.rb" "test/adapter/json_api/has_many_embed_ids_test.rb" "test/adapter/json_api/has_many_explicit_serializer_test.rb" "test/adapter/json_api/has_many_test.rb" "test/adapter/json_api/has_one_test.rb" "test/adapter/json_api/linked_test.rb" "test/adapter/json_test.rb" "test/adapter/null_test.rb" "test/adapter_test.rb" "test/array_serializer_test.rb" "test/serializers/adapter_for_test.rb" "test/serializers/associations_test.rb" "test/serializers/attribute_test.rb" "test/serializers/attributes_test.rb" "test/serializers/cache_test.rb" "test/serializers/configuration_test.rb" "test/serializers/fieldset_test.rb" "test/serializers/generators_test.rb" "test/serializers/meta_test.rb" "test/serializers/options_test.rb" "test/serializers/serializer_for_test.rb" "test/serializers/urls_test.rb" ] /$HOME/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `eval' /$HOME/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `<main>' Tasks: TOP => default => test (See full trace by running task with --trace)
1 parent a5554e0 commit 14439aa

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

test/action_controller/explicit_serializer_test.rb

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,10 @@ def render_array_using_explicit_serializer_and_custom_serializers
5757
end
5858

5959
def render_using_explicit_each_serializer
60-
@comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
61-
@author = Author.new(id: 1, name: 'Joao Moura.')
62-
@post = Post.new({ id: 1, title: 'New Post', body: 'Body', comments: [@comment], author: @author })
60+
location = Location.new(id: 42, lat: '-23.550520', lng: '-46.633309')
61+
place = Place.new(id: 1337, name: 'Amazing Place', locations: [location])
6362

64-
render json: @post, each_serializer: PostSerializer
63+
render json: place, each_serializer: PlaceSerializer
6564
end
6665
end
6766

@@ -118,25 +117,19 @@ def test_render_using_explicit_each_serializer
118117
get :render_using_explicit_each_serializer
119118

120119
expected = {
121-
id: 1,
122-
title: 'New Post',
123-
body: 'Body',
124-
comments: [
120+
id: 1337,
121+
name: "Amazing Place",
122+
locations: [
125123
{
126-
id: 1,
127-
body: 'ZOMG A COMMENT' }
128-
],
129-
blog: {
130-
id: 999,
131-
name: 'Custom blog'
132-
},
133-
author: {
134-
id: 1,
135-
name: 'Joao Moura.'
136-
}
124+
id: 42,
125+
lat: "-23.550520",
126+
lng: "-46.633309",
127+
place: "Nowhere" # is a virtual attribute on LocationSerializer
128+
}
129+
]
137130
}
138131

139-
assert_equal expected.to_json, @response.body
132+
assert_equal expected.to_json, response.body
140133
end
141134
end
142135
end

0 commit comments

Comments
 (0)