https://github.com/leonelgalan/rspec-active_model_serializers
Simple testing of ActiveModelSerializers via a collection of matchers.
https://github.com/leonelgalan/rspec-active_model_serializers
activemodelserializers gem json-schema open-source opensource rspec serialization testing
Last synced: 2 months ago
JSON representation
Simple testing of ActiveModelSerializers via a collection of matchers.
- Host: GitHub
- URL: https://github.com/leonelgalan/rspec-active_model_serializers
- Owner: leonelgalan
- Created: 2017-01-31T22:13:25.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-07-09T15:40:40.000Z (almost 6 years ago)
- Last Synced: 2025-02-28T07:49:37.008Z (3 months ago)
- Topics: activemodelserializers, gem, json-schema, open-source, opensource, rspec, serialization, testing
- Language: Ruby
- Size: 21.5 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# rspec-active_model_serializers
[](https://travis-ci.org/leonelgalan/rspec-active_model_serializers)
[](https://codeclimate.com/github/leonelgalan/rspec-active_model_serializers)
[](https://codeclimate.com/github/leonelgalan/rspec-active_model_serializers/coverage)
[](https://codeclimate.com/github/leonelgalan/rspec-active_model_serializers)
[](https://gemnasium.com/github.com/leonelgalan/rspec-active_model_serializers)
[](https://rubygems.org/gems/rspec-active_model_serializers)## Installation
Add **rspec-active_model_serializers** the `:test` group in the _Gemfile_:
```ruby
group :test do
gem 'rspec-active_model_serializers'
end
```## Usage
### `have_valid_schema`
Validates the request or response against a [JSON
Schema](http://json-schema.org/). You can customize which schema to use by
chaining `.at_path(path_to_schema)`.```ruby
# spec/rails_helper.rb
ActiveModelSerializers.config.schema_path = 'spec/support/schemas'
``````ruby
# 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
endcontext '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](https://github.com/rails-api/active_model_serializers/blob/v0.10.9/lib/active_model_serializers/test/schema.rb)
and [RSpec::ActiveModelSerializers::Matchers::HaveValidSchema](lib/rspec/active_model_serializers/matchers/have_valid_schema.rb)
for additional documentation.