Skip to content

Simple testing of ActiveModelSerializers via a collection of matchers.

Notifications You must be signed in to change notification settings

leonelgalan/rspec-active_model_serializers

Repository files navigation

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.

Packages

No packages published

Languages