rspec-active_model_serializers 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +22 -0
  3. data/.gitignore +10 -0
  4. data/.rspec +2 -0
  5. data/.rubocop.yml +2 -0
  6. data/.travis.yml +10 -0
  7. data/CHANGELOG.md +8 -0
  8. data/Gemfile +5 -0
  9. data/README.md +60 -0
  10. data/lib/rspec/active_model_serializers.rb +10 -0
  11. data/lib/rspec/active_model_serializers/matchers/have_valid_schema.rb +26 -0
  12. data/lib/rspec/active_model_serializers/version.rb +7 -0
  13. data/rspec-active_model_serializers.gemspec +27 -0
  14. data/spec/dummy/Rakefile +6 -0
  15. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  16. data/spec/dummy/app/controllers/profiles_controller.rb +30 -0
  17. data/spec/dummy/app/models/profile.rb +3 -0
  18. data/spec/dummy/app/serializers/profile_serializer.rb +3 -0
  19. data/spec/dummy/config.ru +5 -0
  20. data/spec/dummy/config/application.rb +22 -0
  21. data/spec/dummy/config/boot.rb +5 -0
  22. data/spec/dummy/config/environment.rb +5 -0
  23. data/spec/dummy/config/environments/development.rb +42 -0
  24. data/spec/dummy/config/environments/production.rb +73 -0
  25. data/spec/dummy/config/environments/test.rb +36 -0
  26. data/spec/dummy/config/routes.rb +8 -0
  27. data/spec/dummy/config/secrets.yml +22 -0
  28. data/spec/lib/rspec/active_model_serializers/matchers/have_valid_schema_spec.rb +93 -0
  29. data/spec/rails_helper.rb +21 -0
  30. data/spec/spec_helper.rb +65 -0
  31. data/spec/support/custom_schemas/profiles/index.json +6 -0
  32. data/spec/support/schemas/custom/show.json +7 -0
  33. data/spec/support/schemas/hyper_schema.json +93 -0
  34. data/spec/support/schemas/profiles/index.json +6 -0
  35. data/spec/support/schemas/profiles/show.json +6 -0
  36. data/spec/support/schemas/render_using_json_api.json +43 -0
  37. data/spec/support/schemas/simple_json_pointers.json +10 -0
  38. metadata +208 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: aed72b4555810bd7e10fb9663b219583d141f9d3
4
+ data.tar.gz: a9801d9e149c11bd0dfe3e5eb1abb586a69687cf
5
+ SHA512:
6
+ metadata.gz: 525766be5b566653101c2bc162a078a8937afb911e3cbfca5ef0dce2caad77f00c126034640771400ceeee94c530258d7fa3179b5a5501c392b2d13bb791ed0b
7
+ data.tar.gz: 0e7a8d96507a4d9e1ec103ddbce7000a43c3ed791799888efbaf69d71f751a36490a0fa4c5dca13eab648d0bf66d97d4b88276bb9557dcacbf1098155b125acc
@@ -0,0 +1,22 @@
1
+ ---
2
+ engines:
3
+ duplication:
4
+ enabled: true
5
+ config:
6
+ languages:
7
+ - ruby
8
+ fixme:
9
+ enabled: true
10
+ markdownlint:
11
+ enabled: true
12
+ reek:
13
+ enabled: true
14
+ rubocop:
15
+ enabled: true
16
+ ratings:
17
+ paths:
18
+ - Gemfile.lock
19
+ - "**.rb"
20
+ exclude_paths:
21
+ - coverage/
22
+ - spec/dummy/
@@ -0,0 +1,10 @@
1
+ *.gem
2
+ Gemfile.lock
3
+
4
+ coverage
5
+ doc
6
+
7
+ .ruby-version
8
+
9
+ spec/dummy/log
10
+ spec/examples.txt
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format documentation
@@ -0,0 +1,2 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.3.0
5
+ script:
6
+ - bundle exec rspec
7
+ - bundle exec codeclimate-test-reporter
8
+ addons:
9
+ code_climate:
10
+ repo_token: 671acce37204840dca7f830360332ee9715f1f01bb4c500594b68b709cdc41cb
@@ -0,0 +1,8 @@
1
+ # Change Log
2
+
3
+ ## 1.0.0
4
+
5
+ - Adds `:have_valid_schema` RSpec matcher by
6
+ [@leonelgalan](https://github.com/leonelgalan). Discussed and reviewed on
7
+ [rails-api/active_model_serializers#1947](https://github.com/rails-api/active_model_serializers/pull/1947)
8
+ .
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
@@ -0,0 +1,60 @@
1
+ # rspec-active_model_serializers
2
+
3
+ [![Build Status](https://travis-ci.org/leonelgalan/rspec-active_model_serializers.svg?branch=master)](https://travis-ci.org/leonelgalan/rspec-active_model_serializers)
4
+ [![Code Climate](https://codeclimate.com/github/leonelgalan/rspec-active_model_serializers/badges/gpa.svg)](https://codeclimate.com/github/leonelgalan/rspec-active_model_serializers)
5
+ [![Test Coverage](https://codeclimate.com/github/leonelgalan/rspec-active_model_serializers/badges/coverage.svg)](https://codeclimate.com/github/leonelgalan/rspec-active_model_serializers/coverage)
6
+ [![Issue Count](https://codeclimate.com/github/leonelgalan/rspec-active_model_serializers/badges/issue_count.svg)](https://codeclimate.com/github/leonelgalan/rspec-active_model_serializers)
7
+ [![Dependency Status](https://gemnasium.com/badges/github.com/leonelgalan/rspec-active_model_serializers.svg)](https://gemnasium.com/github.com/leonelgalan/rspec-active_model_serializers)
8
+ [![Gem](https://img.shields.io/gem/v/rspec-active_model_serializers.svg)](https://rubygems.org/gems/rspec-active_model_serializers)
9
+
10
+ ## Installation
11
+
12
+ Add **rspec-active_model_serializers** the `:test` group in the _Gemfile_:
13
+
14
+ ```ruby
15
+ group :test do
16
+ gem 'rspec-active_model_serializers'
17
+ end
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ ### `have_valid_schema`
23
+
24
+ Validates the request or response against a [JSON
25
+ Schema](http://json-schema.org/). You can customize which schema to use by
26
+ chaining `.at_path(path_to_schema)`.
27
+
28
+ ```ruby
29
+ # spec/rails_helper.rb
30
+ ActiveModelSerializers.config.schema_path = 'spec/support/schemas'
31
+ ```
32
+
33
+ ```ruby
34
+ # spec/controller/posts_controller_spec.rb
35
+ Rspec.describe PostsController do
36
+ context 'GET /index' do
37
+ it 'responds with a valid schema' do
38
+ get :index
39
+ # Schema: spec/support/schemas/posts/index.json
40
+ expect(response).to have_valid_schema
41
+ end
42
+ end
43
+
44
+ context 'GET /show' do
45
+ it 'responds with a valid schema' do
46
+ get :show, id: 1
47
+ # Schema: spec/support/schemas/custom/show.json
48
+ expect(response).to have_valid_schema.at_path('custom/show.json')
49
+ end
50
+ end
51
+ end
52
+ ```
53
+
54
+ `expect(response_or_request).to have_valid_schema.at_path(path_to_schema)` can
55
+ be understood as being a translation of both
56
+ `assert_response_schema(path_to_schema)` and
57
+ `assert_request_schema(path_to_schema)`. See
58
+ [ActiveModelSerializers::Test::Schema](../../lib/active_model_serializers/test/schema.rb)
59
+ and [RSpec::ActiveModelSerializers::Matchers::HaveValidSchema](../../lib/rspec/active_model_serializers/matchers/have_valid_schema.rb)
60
+ for additional documentation.
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rspec/active_model_serializers/matchers/have_valid_schema'
4
+
5
+ RSpec.configure do |config|
6
+ config.include RSpec::ActiveModelSerializers::Matchers::HaveValidSchema,
7
+ type: :request
8
+ config.include RSpec::ActiveModelSerializers::Matchers::HaveValidSchema,
9
+ type: :controller
10
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_model_serializers'
4
+
5
+ AssertSchema = ActiveModelSerializers::Test::Schema::AssertSchema
6
+
7
+ module RSpec
8
+ module ActiveModelSerializers
9
+ module Matchers
10
+ # HaveValidSchema
11
+ # Including this module defines the :have_valid_schema rspec expectations
12
+ module HaveValidSchema
13
+ RSpec::Matchers.define :have_valid_schema do
14
+ chain :at_path do |schema_path|
15
+ @schema_path = schema_path
16
+ end
17
+
18
+ match do
19
+ @matcher = AssertSchema.new(@schema_path, request, response, nil)
20
+ @matcher.call
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RSpec
4
+ module ActiveModelSerializers
5
+ VERSION = '1.0.0'
6
+ end
7
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path('../lib/rspec/active_model_serializers/version',
4
+ __FILE__)
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'rspec-active_model_serializers'
8
+ spec.version = RSpec::ActiveModelSerializers::VERSION
9
+ spec.authors = ['Leonel Galan']
10
+ spec.email = ['leonelgalan@gmail.com']
11
+ spec.homepage = 'http://github.com/leonelgalan/rspec-active_model_serializers'
12
+ spec.summary = 'RSpec for ActiveModelSerializers'
13
+ spec.description = 'Simple testing of ActiveModelSerializers via a ' \
14
+ 'collection of matchers.'
15
+ spec.license = 'MIT'
16
+ spec.add_runtime_dependency 'rspec', '~> 3.5'
17
+ spec.add_runtime_dependency 'minitest', '~> 5.1'
18
+ spec.add_runtime_dependency 'active_model_serializers', '~> 0.10.4'
19
+ spec.add_runtime_dependency 'json_schema', '~> 0.16'
20
+
21
+ spec.add_development_dependency 'railties', ['>= 4.1', '< 6']
22
+ spec.add_development_dependency 'rspec-rails', '~> 3.5'
23
+ spec.add_development_dependency 'codeclimate-test-reporter', '~> 1.0'
24
+ spec.files = `git ls-files`.split("\n")
25
+ spec.test_files = `git ls-files -- spec/*`.split("\n")
26
+ spec.require_paths = ['lib']
27
+ end
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require_relative 'config/application'
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery with: :exception
3
+ end
@@ -0,0 +1,30 @@
1
+ class ProfilesController < ApplicationController
2
+ before_action :set_profile
3
+
4
+ def index
5
+ render json: @profile
6
+ end
7
+
8
+ def show
9
+ index
10
+ end
11
+
12
+ def name_as_a_integer
13
+ @profile.attributes[:name] = 1
14
+ index
15
+ end
16
+
17
+ def render_using_json_api
18
+ render json: @profile, adapter: :json_api
19
+ end
20
+
21
+ def invalid_json_body
22
+ render json: ''
23
+ end
24
+
25
+ private
26
+
27
+ def set_profile
28
+ @profile = Profile.new(name: 'Name 1', description: 'Description 1')
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ class Profile < ActiveModelSerializers::Model
2
+ attr_accessor :name, :description
3
+ end
@@ -0,0 +1,3 @@
1
+ class ProfileSerializer < ActiveModel::Serializer
2
+ attributes :name, :description
3
+ end
@@ -0,0 +1,5 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require_relative 'config/environment'
4
+
5
+ run Rails.application
@@ -0,0 +1,22 @@
1
+ require_relative 'boot'
2
+
3
+ # Pick the frameworks you want:
4
+ # require "active_record/railtie"
5
+ require "action_controller/railtie"
6
+ require "action_view/railtie"
7
+ # require "action_mailer/railtie"
8
+ # require "active_job/railtie"
9
+ # require "action_cable/engine"
10
+ require "rails/test_unit/railtie"
11
+ # require "sprockets/railtie"
12
+
13
+ Bundler.require(*Rails.groups)
14
+ require "active_model_serializers"
15
+
16
+ module Dummy
17
+ class Application < Rails::Application
18
+ # Settings in config/environments/* take precedence over those specified here.
19
+ # Application configuration should go into files in config/initializers
20
+ # -- all .rb files in that directory are automatically loaded.
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ # Set up gems listed in the Gemfile.
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../Gemfile', __dir__)
3
+
4
+ require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
5
+ $LOAD_PATH.unshift File.expand_path('../../../lib', __dir__)
@@ -0,0 +1,5 @@
1
+ # Load the Rails application.
2
+ require_relative 'application'
3
+
4
+ # Initialize the Rails application.
5
+ Rails.application.initialize!
@@ -0,0 +1,42 @@
1
+ Rails.application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb.
3
+
4
+ # In the development environment your application's code is reloaded on
5
+ # every request. This slows down response time but is perfect for development
6
+ # since you don't have to restart the web server when you make code changes.
7
+ config.cache_classes = false
8
+
9
+ # Do not eager load code on boot.
10
+ config.eager_load = false
11
+
12
+ # Show full error reports.
13
+ config.consider_all_requests_local = true
14
+
15
+ # Enable/disable caching. By default caching is disabled.
16
+ if Rails.root.join('tmp/caching-dev.txt').exist?
17
+ config.action_controller.perform_caching = true
18
+
19
+ config.cache_store = :memory_store
20
+ config.public_file_server.headers = {
21
+ 'Cache-Control' => 'public, max-age=172800'
22
+ }
23
+ else
24
+ config.action_controller.perform_caching = false
25
+
26
+ config.cache_store = :null_store
27
+ end
28
+
29
+ # Print deprecation notices to the Rails logger.
30
+ config.active_support.deprecation = :log
31
+
32
+ # Raise an error on page load if there are pending migrations.
33
+ # config.active_record.migration_error = :page_load
34
+
35
+
36
+ # Raises error for missing translations
37
+ # config.action_view.raise_on_missing_translations = true
38
+
39
+ # Use an evented file watcher to asynchronously detect changes in source code,
40
+ # routes, locales, etc. This feature depends on the listen gem.
41
+ # config.file_watcher = ActiveSupport::EventedFileUpdateChecker
42
+ end
@@ -0,0 +1,73 @@
1
+ Rails.application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb.
3
+
4
+ # Code is not reloaded between requests.
5
+ config.cache_classes = true
6
+
7
+ # Eager load code on boot. This eager loads most of Rails and
8
+ # your application in memory, allowing both threaded web servers
9
+ # and those relying on copy on write to perform better.
10
+ # Rake tasks automatically ignore this option for performance.
11
+ config.eager_load = true
12
+
13
+ # Full error reports are disabled and caching is turned on.
14
+ config.consider_all_requests_local = false
15
+ config.action_controller.perform_caching = true
16
+
17
+ # Disable serving static files from the `/public` folder by default since
18
+ # Apache or NGINX already handles this.
19
+ config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
20
+
21
+
22
+ # Enable serving of images, stylesheets, and JavaScripts from an asset server.
23
+ # config.action_controller.asset_host = 'http://assets.example.com'
24
+
25
+ # Specifies the header that your server uses for sending files.
26
+ # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
27
+ # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX
28
+
29
+ # Mount Action Cable outside main process or domain
30
+ # config.action_cable.mount_path = nil
31
+ # config.action_cable.url = 'wss://example.com/cable'
32
+ # config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ]
33
+
34
+ # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
35
+ # config.force_ssl = true
36
+
37
+ # Use the lowest log level to ensure availability of diagnostic information
38
+ # when problems arise.
39
+ config.log_level = :debug
40
+
41
+ # Prepend all log lines with the following tags.
42
+ config.log_tags = [ :request_id ]
43
+
44
+ # Use a different cache store in production.
45
+ # config.cache_store = :mem_cache_store
46
+
47
+ # Use a real queuing backend for Active Job (and separate queues per environment)
48
+ # config.active_job.queue_adapter = :resque
49
+ # config.active_job.queue_name_prefix = "dummy_#{Rails.env}"
50
+
51
+ # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
52
+ # the I18n.default_locale when a translation cannot be found).
53
+ config.i18n.fallbacks = true
54
+
55
+ # Send deprecation notices to registered listeners.
56
+ config.active_support.deprecation = :notify
57
+
58
+ # Use default logging formatter so that PID and timestamp are not suppressed.
59
+ config.log_formatter = ::Logger::Formatter.new
60
+
61
+ # Use a different logger for distributed setups.
62
+ # require 'syslog/logger'
63
+ # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')
64
+
65
+ if ENV["RAILS_LOG_TO_STDOUT"].present?
66
+ logger = ActiveSupport::Logger.new(STDOUT)
67
+ logger.formatter = config.log_formatter
68
+ config.logger = ActiveSupport::TaggedLogging.new(logger)
69
+ end
70
+
71
+ # Do not dump schema after migrations.
72
+ config.active_record.dump_schema_after_migration = false
73
+ end
@@ -0,0 +1,36 @@
1
+ Rails.application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb.
3
+
4
+ # The test environment is used exclusively to run your application's
5
+ # test suite. You never need to work with it otherwise. Remember that
6
+ # your test database is "scratch space" for the test suite and is wiped
7
+ # and recreated between test runs. Don't rely on the data there!
8
+ config.cache_classes = true
9
+
10
+ # Do not eager load code on boot. This avoids loading your whole application
11
+ # just for the purpose of running a single test. If you are using a tool that
12
+ # preloads Rails for running tests, you may have to set it to true.
13
+ config.eager_load = false
14
+
15
+ # Configure public file server for tests with Cache-Control for performance.
16
+ config.public_file_server.enabled = true
17
+ config.public_file_server.headers = {
18
+ 'Cache-Control' => 'public, max-age=3600'
19
+ }
20
+
21
+ # Show full error reports and disable caching.
22
+ config.consider_all_requests_local = true
23
+ config.action_controller.perform_caching = false
24
+
25
+ # Raise exceptions instead of rendering exception templates.
26
+ config.action_dispatch.show_exceptions = false
27
+
28
+ # Disable request forgery protection in test environment.
29
+ config.action_controller.allow_forgery_protection = false
30
+
31
+ # Print deprecation notices to the stderr.
32
+ config.active_support.deprecation = :stderr
33
+
34
+ # Raises error for missing translations
35
+ # config.action_view.raise_on_missing_translations = true
36
+ end
@@ -0,0 +1,8 @@
1
+ Rails.application.routes.draw do
2
+ namespace 'profiles' do
3
+ %i(index show name_as_a_integer render_using_json_api
4
+ invalid_json_body).each do |action|
5
+ get action, as: action
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,22 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key is used for verifying the integrity of signed cookies.
4
+ # If you change this key, all old signed cookies will become invalid!
5
+
6
+ # Make sure the secret is at least 30 characters and all random,
7
+ # no regular words or you'll be exposed to dictionary attacks.
8
+ # You can use `rails secret` to generate a secure secret key.
9
+
10
+ # Make sure the secrets in this file are kept private
11
+ # if you're sharing your code publicly.
12
+
13
+ development:
14
+ secret_key_base: 4350eb7daa2658196e267822d575126fda86cf684fbaaf969e1a89e559e862c193fcb32cb59723358eb16c4e36a4410da25e6bb40b1cc01147c8fe7c6740111f
15
+
16
+ test:
17
+ secret_key_base: 419a2c7efd9d8a1b93ae5df93f9ecbc3eb5193656f91b2ee446d490f7a870c7e65e4a1f46abaf9c7e5140ab8ca9eccf631dea0609eff99bb89a2be290180da7d
18
+
19
+ # Do not keep production secrets in the repository,
20
+ # instead read values from the environment.
21
+ production:
22
+ secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ RSpec.describe ProfilesController, type: :controller do
6
+ context 'using the schemas in the default schema_path' do
7
+ it 'validates :index response with profile/index.json ' do
8
+ get :index
9
+ expect(response).to have_valid_schema
10
+ end
11
+
12
+ context "doesn't validate :show with profiles/show.json " do
13
+ before { get :show }
14
+
15
+ it 'is expected not to validate' do
16
+ expect(response).not_to have_valid_schema
17
+ end
18
+
19
+ it 'allows to customize the failure message' do
20
+ message = 'oh boy the show is broken'
21
+ expect { expect(response).to have_valid_schema, message }.to(
22
+ raise_error(RSpec::Expectations::ExpectationNotMetError, message)
23
+ )
24
+ end
25
+ end
26
+ end
27
+
28
+ context 'customizing the schema_path globally' do
29
+ around(:example) do |example|
30
+ original_schema_path = ActiveModelSerializers.config.schema_path
31
+ ActiveModelSerializers.config.schema_path = 'spec/support/custom_schemas'
32
+ example.run
33
+ ActiveModelSerializers.config.schema_path = original_schema_path
34
+ end
35
+
36
+ it 'validates :index with custom_schemas/profiles/index.json' do
37
+ get :index
38
+ expect(response).to have_valid_schema
39
+ end
40
+ end
41
+
42
+ context 'overrides schema_path passing it on every expectation' do
43
+ it 'validates :show with custom/show.json' do
44
+ get :show
45
+ expect(response).to have_valid_schema.at_path 'custom/show.json'
46
+ end
47
+
48
+ it 'validates :show with a hyper schema' do
49
+ get :show
50
+ expect(response).to have_valid_schema.at_path 'hyper_schema.json'
51
+ end
52
+
53
+ it 'validates :show with a simple json pointers schema' do
54
+ get :show
55
+ expect(response).to have_valid_schema.at_path 'simple_json_pointers.json'
56
+ end
57
+
58
+ it "doesn't validate :name_as_integer with a simple pointers schema " do
59
+ get :name_as_a_integer
60
+ expect do
61
+ expect(response).to(
62
+ have_valid_schema.at_path('simple_json_pointers.json')
63
+ )
64
+ end.to raise_error(RSpec::Expectations::ExpectationNotMetError)
65
+ end
66
+
67
+ it 'validates :render_using_json_api with the proper schema' do
68
+ get :render_using_json_api
69
+ expect(response).to have_valid_schema.at_path 'render_using_json_api.json'
70
+ end
71
+
72
+ context 'errors' do
73
+ it 'raises error with non-existent schema' do
74
+ message = 'No Schema file at spec/support/schemas/non-existent.json'
75
+ get :show
76
+ expect do
77
+ expect(response).to have_valid_schema.at_path 'non-existent.json'
78
+ end.to raise_error(ActiveModelSerializers::Test::Schema::MissingSchema,
79
+ message)
80
+ end
81
+
82
+ it 'raises error with invalid response (not JSON)' do
83
+ get :invalid_json_body
84
+ expect do
85
+ expect(response).to have_valid_schema.at_path('custom/show.json')
86
+ end.to raise_error(
87
+ ActiveModelSerializers::Test::Schema::InvalidSchemaError,
88
+ /A JSON text must at least contain two octets|unexpected token at ''/
89
+ )
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'simplecov'
4
+ SimpleCov.start do
5
+ add_filter '/spec/'
6
+ end
7
+
8
+ ENV['RAILS_ENV'] ||= 'test'
9
+
10
+ ENGINE_ROOT = File.join(File.dirname(__FILE__), '../')
11
+
12
+ # Load environment.rb from the dummy app.
13
+ require File.expand_path('../dummy/config/environment', __FILE__)
14
+
15
+ abort('Rails is running in production!') if Rails.env.production?
16
+
17
+ require 'spec_helper'
18
+ require 'rspec/rails'
19
+ require 'rspec/active_model_serializers'
20
+
21
+ ActiveModelSerializers.config.schema_path = 'spec/support/schemas'
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.configure do |config|
4
+ # RSpec 4 Defaults
5
+ config.expect_with :rspec do |expectations|
6
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
7
+ end
8
+
9
+ config.mock_with :rspec do |mocks|
10
+ mocks.verify_partial_doubles = true
11
+ end
12
+
13
+ config.shared_context_metadata_behavior = :apply_to_host_groups
14
+
15
+ # Suggested Settings
16
+ # This allows you to limit a spec run to individual examples or groups
17
+ # you care about by tagging them with `:focus` metadata. When nothing
18
+ # is tagged with `:focus`, all examples get run. RSpec also provides
19
+ # aliases for `it`, `describe`, and `context` that include `:focus`
20
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
21
+ config.filter_run_when_matching :focus
22
+
23
+ # Allows RSpec to persist some state between runs in order to support
24
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
25
+ # you configure your source control system to ignore this file.
26
+ config.example_status_persistence_file_path = 'spec/examples.txt'
27
+
28
+ # Limits the available syntax to the non-monkey patched syntax that is
29
+ # recommended. For more details, see:
30
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
31
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
32
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
33
+ config.disable_monkey_patching!
34
+
35
+ # This setting enables warnings. It's recommended, but in some cases may
36
+ # be too noisy due to issues in dependencies.
37
+ # config.warnings = true
38
+
39
+ # Many RSpec users commonly either run the entire suite or an individual
40
+ # file, and it's useful to allow more verbose output when running an
41
+ # individual spec file.
42
+ if config.files_to_run.one?
43
+ # Use the documentation formatter for detailed output,
44
+ # unless a formatter has already been configured
45
+ # (e.g. via a command-line flag).
46
+ config.default_formatter = 'doc'
47
+ end
48
+
49
+ # Print the 10 slowest examples and example groups at the
50
+ # end of the spec run, to help surface which specs are running
51
+ # particularly slow.
52
+ config.profile_examples = 10
53
+
54
+ # Run specs in random order to surface order dependencies. If you find an
55
+ # order dependency and want to debug it, you can fix the order by providing
56
+ # the seed, which is printed after each run.
57
+ # --seed 1234
58
+ config.order = :random
59
+
60
+ # Seed global randomization in this process using the `--seed` CLI option.
61
+ # Setting this allows you to use `--seed` to deterministically reproduce
62
+ # test failures related to randomization by passing the same `--seed` value
63
+ # as the one that triggered the failure.
64
+ Kernel.srand config.seed
65
+ end
@@ -0,0 +1,6 @@
1
+ {
2
+ "properties": {
3
+ "name" : { "type" : "string" },
4
+ "description" : { "type" : "string" }
5
+ }
6
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "id": "file://custom/show.json#",
3
+ "properties": {
4
+ "name" : { "type" : "string" },
5
+ "description" : { "type" : "string" }
6
+ }
7
+ }
@@ -0,0 +1,93 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-04/hyper-schema",
3
+ "title": "Profile",
4
+ "description": "Profile schema",
5
+ "stability": "prototype",
6
+ "strictProperties": true,
7
+ "type": [
8
+ "object"
9
+ ],
10
+ "definitions": {
11
+ "name": {
12
+ "description": "unique name of profile",
13
+ "readOnly": true,
14
+ "type": [
15
+ "string"
16
+ ]
17
+ },
18
+ "description": {
19
+ "description": "description of profile",
20
+ "readOnly": true,
21
+ "type": [
22
+ "string"
23
+ ]
24
+ },
25
+ "identity": {
26
+ "anyOf": [
27
+ {
28
+ "$ref": "/schemata/profile#/definitions/name"
29
+ }
30
+ ]
31
+ }
32
+ },
33
+ "links": [
34
+ {
35
+ "description": "Create a new profile.",
36
+ "href": "/profiles",
37
+ "method": "POST",
38
+ "rel": "create",
39
+ "schema": {
40
+ "properties": {
41
+ },
42
+ "type": [
43
+ "object"
44
+ ]
45
+ },
46
+ "title": "Create"
47
+ },
48
+ {
49
+ "description": "Delete an existing profile.",
50
+ "href": "/profiles/{(%2Fschemata%2Fprofile%23%2Fdefinitions%2Fidentity)}",
51
+ "method": "DELETE",
52
+ "rel": "destroy",
53
+ "title": "Delete"
54
+ },
55
+ {
56
+ "description": "Info for existing profile.",
57
+ "href": "/profiles/{(%2Fschemata%2Fprofile%23%2Fdefinitions%2Fidentity)}",
58
+ "method": "GET",
59
+ "rel": "self",
60
+ "title": "Info"
61
+ },
62
+ {
63
+ "description": "List existing profiles.",
64
+ "href": "/profiles",
65
+ "method": "GET",
66
+ "rel": "instances",
67
+ "title": "List"
68
+ },
69
+ {
70
+ "description": "Update an existing profile.",
71
+ "href": "/profiles/{(%2Fschemata%2Fprofile%23%2Fdefinitions%2Fidentity)}",
72
+ "method": "PATCH",
73
+ "rel": "update",
74
+ "schema": {
75
+ "properties": {
76
+ },
77
+ "type": [
78
+ "object"
79
+ ]
80
+ },
81
+ "title": "Update"
82
+ }
83
+ ],
84
+ "properties": {
85
+ "name": {
86
+ "$ref": "/schemata/profile#/definitions/name"
87
+ },
88
+ "description": {
89
+ "$ref": "/schemata/profile#/definitions/description"
90
+ }
91
+ },
92
+ "id": "/schemata/profile"
93
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "properties": {
3
+ "name" : { "type" : "string" },
4
+ "description" : { "type" : "string" }
5
+ }
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "properties": {
3
+ "name" : { "type" : "integer" },
4
+ "description" : { "type" : "boolean" }
5
+ }
6
+ }
@@ -0,0 +1,43 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-04/schema#",
3
+ "id": "",
4
+ "type": "object",
5
+ "properties": {
6
+ "data": {
7
+ "id": "/data",
8
+ "type": "object",
9
+ "properties": {
10
+ "id": {
11
+ "id": "/data/id",
12
+ "type": "string"
13
+ },
14
+ "type": {
15
+ "id": "/data/type",
16
+ "type": "string"
17
+ },
18
+ "attributes": {
19
+ "id": "/data/attributes",
20
+ "type": "object",
21
+ "properties": {
22
+ "name": {
23
+ "id": "/data/attributes/name",
24
+ "type": "string"
25
+ },
26
+ "description": {
27
+ "id": "/data/attributes/description",
28
+ "type": "string"
29
+ }
30
+ }
31
+ }
32
+ },
33
+ "required": [
34
+ "id",
35
+ "type",
36
+ "attributes"
37
+ ]
38
+ }
39
+ },
40
+ "required": [
41
+ "data"
42
+ ]
43
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "properties": {
3
+ "name": {
4
+ "$ref": "file://custom/show.json#/properties/name"
5
+ },
6
+ "description": {
7
+ "$ref": "file://custom/show.json#/properties/description"
8
+ }
9
+ }
10
+ }
metadata ADDED
@@ -0,0 +1,208 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-active_model_serializers
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Leonel Galan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: active_model_serializers
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.10.4
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.10.4
55
+ - !ruby/object:Gem::Dependency
56
+ name: json_schema
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.16'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.16'
69
+ - !ruby/object:Gem::Dependency
70
+ name: railties
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '4.1'
76
+ - - "<"
77
+ - !ruby/object:Gem::Version
78
+ version: '6'
79
+ type: :development
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '4.1'
86
+ - - "<"
87
+ - !ruby/object:Gem::Version
88
+ version: '6'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rspec-rails
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '3.5'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '3.5'
103
+ - !ruby/object:Gem::Dependency
104
+ name: codeclimate-test-reporter
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '1.0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '1.0'
117
+ description: Simple testing of ActiveModelSerializers via a collection of matchers.
118
+ email:
119
+ - leonelgalan@gmail.com
120
+ executables: []
121
+ extensions: []
122
+ extra_rdoc_files: []
123
+ files:
124
+ - ".codeclimate.yml"
125
+ - ".gitignore"
126
+ - ".rspec"
127
+ - ".rubocop.yml"
128
+ - ".travis.yml"
129
+ - CHANGELOG.md
130
+ - Gemfile
131
+ - README.md
132
+ - lib/rspec/active_model_serializers.rb
133
+ - lib/rspec/active_model_serializers/matchers/have_valid_schema.rb
134
+ - lib/rspec/active_model_serializers/version.rb
135
+ - rspec-active_model_serializers.gemspec
136
+ - spec/dummy/Rakefile
137
+ - spec/dummy/app/controllers/application_controller.rb
138
+ - spec/dummy/app/controllers/profiles_controller.rb
139
+ - spec/dummy/app/models/profile.rb
140
+ - spec/dummy/app/serializers/profile_serializer.rb
141
+ - spec/dummy/config.ru
142
+ - spec/dummy/config/application.rb
143
+ - spec/dummy/config/boot.rb
144
+ - spec/dummy/config/environment.rb
145
+ - spec/dummy/config/environments/development.rb
146
+ - spec/dummy/config/environments/production.rb
147
+ - spec/dummy/config/environments/test.rb
148
+ - spec/dummy/config/routes.rb
149
+ - spec/dummy/config/secrets.yml
150
+ - spec/lib/rspec/active_model_serializers/matchers/have_valid_schema_spec.rb
151
+ - spec/rails_helper.rb
152
+ - spec/spec_helper.rb
153
+ - spec/support/custom_schemas/profiles/index.json
154
+ - spec/support/schemas/custom/show.json
155
+ - spec/support/schemas/hyper_schema.json
156
+ - spec/support/schemas/profiles/index.json
157
+ - spec/support/schemas/profiles/show.json
158
+ - spec/support/schemas/render_using_json_api.json
159
+ - spec/support/schemas/simple_json_pointers.json
160
+ homepage: http://github.com/leonelgalan/rspec-active_model_serializers
161
+ licenses:
162
+ - MIT
163
+ metadata: {}
164
+ post_install_message:
165
+ rdoc_options: []
166
+ require_paths:
167
+ - lib
168
+ required_ruby_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ requirements: []
179
+ rubyforge_project:
180
+ rubygems_version: 2.5.1
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: RSpec for ActiveModelSerializers
184
+ test_files:
185
+ - spec/dummy/Rakefile
186
+ - spec/dummy/app/controllers/application_controller.rb
187
+ - spec/dummy/app/controllers/profiles_controller.rb
188
+ - spec/dummy/app/models/profile.rb
189
+ - spec/dummy/app/serializers/profile_serializer.rb
190
+ - spec/dummy/config.ru
191
+ - spec/dummy/config/application.rb
192
+ - spec/dummy/config/boot.rb
193
+ - spec/dummy/config/environment.rb
194
+ - spec/dummy/config/environments/development.rb
195
+ - spec/dummy/config/environments/production.rb
196
+ - spec/dummy/config/environments/test.rb
197
+ - spec/dummy/config/routes.rb
198
+ - spec/dummy/config/secrets.yml
199
+ - spec/lib/rspec/active_model_serializers/matchers/have_valid_schema_spec.rb
200
+ - spec/rails_helper.rb
201
+ - spec/spec_helper.rb
202
+ - spec/support/custom_schemas/profiles/index.json
203
+ - spec/support/schemas/custom/show.json
204
+ - spec/support/schemas/hyper_schema.json
205
+ - spec/support/schemas/profiles/index.json
206
+ - spec/support/schemas/profiles/show.json
207
+ - spec/support/schemas/render_using_json_api.json
208
+ - spec/support/schemas/simple_json_pointers.json