Skip to content

Instantly share code, notes, and snippets.

@nuqz
Last active December 8, 2016 22:07
Show Gist options
  • Save nuqz/7ba2bae9ad65606983b72e2f80a80680 to your computer and use it in GitHub Desktop.
Save nuqz/7ba2bae9ad65606983b72e2f80a80680 to your computer and use it in GitHub Desktop.
RSpec matcher to test JSON API Controller output provided with ActiveModel Serializer.
# Encountered an error when tested console output:
#
# ActiveModelSerializers::Adapter::JsonApi::PaginationLinks::MissingSerializationContextError: JsonApi::PaginationLinks requires a ActiveModelSerializers::SerializationContext.
# Please pass a ':serialization_context' option or
# override CollectionSerializer#paginated? to return 'false'.
# Usual RSpec matcher definition
RSpec::Matchers.define :render_serialized_json_data do |expected|
match do |actual|
# Prepairing serialized JSON from ActiveRecord(s)
expected = ActiveModelSerializers::SerializableResource
.new(expected, {
# The next line is to provide pagination outside Controller
:serialization_context => ActiveModelSerializers::SerializationContext.new(request, {})
}).as_json.to_json
# actual is expected to be 'response.body'
expect(actual).to eq expected
end
description do
"render serialized json data"
end
diffable
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment