No commit activity in last 3 years
No release in over 3 years
Simple testing of ActiveModelSerializers via a collection of matchers.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

rspec-active_model_serializers

Build Status Code Climate Test Coverage Issue Count Dependency Status Gem

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.