|
1 | 1 | # How to test |
2 | 2 |
|
3 | | -## Test helpers |
| 3 | +## Controller Serializer Usage |
4 | 4 |
|
5 | 5 | ActiveModelSerializers provides a `assert_serializer` method to be used on your controller tests to |
6 | 6 | assert that a specific serializer was used. |
|
16 | 16 |
|
17 | 17 | See [ActiveModelSerializers::Test::Serializer](../../lib/active_model_serializers/test/serializer.rb) |
18 | 18 | for more examples and documentation. |
| 19 | + |
| 20 | +## Serialization against a schema |
| 21 | + |
| 22 | +### Dependencies |
| 23 | + |
| 24 | +To use the `assert_response_schema` you need to have the |
| 25 | +[`json_schema`](https://github.com/brandur/json_schema) on your Gemfile. Please |
| 26 | +add it to your Gemfile and run `$ bundle install`. |
| 27 | + |
| 28 | +### Minitest test helpers |
| 29 | + |
| 30 | +ActiveModelSerializers provides a `assert_response_schema` method to be used on your controller tests to |
| 31 | +assert the response against a [JSON Schema](http://json-schema.org/). Let's take |
| 32 | +a look in an example. |
| 33 | + |
| 34 | +```ruby |
| 35 | +class PostsController < ApplicationController |
| 36 | + def show |
| 37 | + @post = Post.find(params[:id]) |
| 38 | + |
| 39 | + render json: @post |
| 40 | + end |
| 41 | +end |
| 42 | +``` |
| 43 | + |
| 44 | +To test the `posts#show` response of this controller we need to create a file |
| 45 | +named `test/support/schemas/posts/show.json`. The helper uses a naming convention |
| 46 | +to locate the file. |
| 47 | + |
| 48 | +This file is a JSON Schema representation of our response. |
| 49 | + |
| 50 | +```json |
| 51 | +{ |
| 52 | + "properties": { |
| 53 | + "title" : { "type" : "string" }, |
| 54 | + "content" : { "type" : "string" } |
| 55 | + } |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +With all in place we can go to our test and use the helper. |
| 60 | + |
| 61 | +```ruby |
| 62 | +class PostsControllerTest < ActionController::TestCase |
| 63 | + test "should render right response" do |
| 64 | + get :index |
| 65 | + assert_response_schema |
| 66 | + end |
| 67 | +end |
| 68 | +``` |
| 69 | + |
| 70 | +#### Load a custom schema |
| 71 | + |
| 72 | +If we need to use another schema, for example when we have a namespaced API that |
| 73 | +shows the same response, we can pass the path of the schema. |
| 74 | + |
| 75 | +```ruby |
| 76 | +module V1 |
| 77 | + class PostsController < ApplicationController |
| 78 | + def show |
| 79 | + @post = Post.find(params[:id]) |
| 80 | + |
| 81 | + render json: @post |
| 82 | + end |
| 83 | + end |
| 84 | +end |
| 85 | +``` |
| 86 | + |
| 87 | +```ruby |
| 88 | +class V1::PostsControllerTest < ActionController::TestCase |
| 89 | + test "should render right response" do |
| 90 | + get :index |
| 91 | + assert_response_schema('posts/show.json') |
| 92 | + end |
| 93 | +end |
| 94 | +``` |
| 95 | + |
| 96 | +#### Change the schema path |
| 97 | + |
| 98 | +By default all schemas are created at `test/support/schemas`. If we are using |
| 99 | +RSpec for example we can change this to `spec/support/schemas` defining the |
| 100 | +default schema path in an initializer. |
| 101 | + |
| 102 | +```ruby |
| 103 | +ActiveModelSerializers.config.schema_path = 'spec/support/schemas' |
| 104 | +``` |
| 105 | + |
| 106 | +#### Using with the Heroku’s JSON Schema-based tools |
| 107 | + |
| 108 | +To use the test helper with the [prmd](https://github.com/interagent/prmd) and |
| 109 | +[committee](https://github.com/interagent/committee). |
| 110 | + |
| 111 | +We need to change the schema path to the recommended by prmd: |
| 112 | + |
| 113 | +```ruby |
| 114 | +ActiveModelSerializers.config.schema_path = 'docs/schema/schemata' |
| 115 | +``` |
| 116 | + |
| 117 | +We also need to structure our schemata according to Heroku's conventions |
| 118 | +(e.g. including |
| 119 | +[required metadata](https://github.com/interagent/prmd/blob/master/docs/schemata.md#meta-data) |
| 120 | +and [links](https://github.com/interagent/prmd/blob/master/docs/schemata.md#links). |
| 121 | + |
| 122 | +#### JSON Pointers |
| 123 | + |
| 124 | +If we plan to use [JSON |
| 125 | +Pointers](http://spacetelescope.github.io/understanding-json-schema/UnderstandingJSONSchema.pdf) we need to define the `id` attribute on the schema. Example: |
| 126 | + |
| 127 | +```js |
| 128 | +// attributes.json |
| 129 | + |
| 130 | +{ |
| 131 | + "id": "file://attributes.json#", |
| 132 | + "properties": { |
| 133 | + "name" : { "type" : "string" }, |
| 134 | + "description" : { "type" : "string" } |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +```js |
| 140 | +// show.json |
| 141 | + |
| 142 | +{ |
| 143 | + "properties": { |
| 144 | + "name": { |
| 145 | + "$ref": "file://attributes.json#/properties/name" |
| 146 | + }, |
| 147 | + "description": { |
| 148 | + "$ref": "file://attributes.json#/properties/description" |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | +``` |
0 commit comments