Last active
December 8, 2016 22:07
-
-
Save nuqz/7ba2bae9ad65606983b72e2f80a80680 to your computer and use it in GitHub Desktop.
RSpec matcher to test JSON API Controller output provided with ActiveModel Serializer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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