rspec-active_model_serializers
Installation
Add rspec-active_model_serializers the :test
group in the Gemfile:
group :test do
gem 'rspec-active_model_serializers'
end
Usage
have_valid_schema
Validates the request or response against a JSON
Schema. You can customize which schema to use by
chaining .at_path(path_to_schema)
.
# spec/rails_helper.rb
ActiveModelSerializers.config.schema_path = 'spec/support/schemas'
# spec/controller/posts_controller_spec.rb
Rspec.describe PostsController do
context 'GET /index' do
it 'responds with a valid schema' do
get :index
# Schema: spec/support/schemas/posts/index.json
expect(response).to have_valid_schema
end
end
context 'GET /show' do
it 'responds with a valid schema' do
get :show, id: 1
# Schema: spec/support/schemas/custom/show.json
expect(response).to have_valid_schema.at_path('custom/show.json')
end
end
end
expect(response_or_request).to have_valid_schema.at_path(path_to_schema)
can
be understood as being a translation of both
assert_response_schema(path_to_schema)
and
assert_request_schema(path_to_schema)
. See
ActiveModelSerializers::Test::Schema
and RSpec::ActiveModelSerializers::Matchers::HaveValidSchema
for additional documentation.