File: test.md

package info (click to toggle)
ruby-active-model-serializers 0.10.12-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,752 kB
  • sloc: ruby: 13,138; sh: 53; makefile: 6
file content (154 lines) | stat: -rw-r--r-- 3,746 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
[Back to Guides](../README.md)

# How to test

## Controller Serializer Usage

ActiveModelSerializers provides a `assert_serializer` method to be used on your controller tests to
assert that a specific serializer was used.

```ruby
class PostsControllerTest < ActionController::TestCase
  test "should render post serializer" do
    get :index
    assert_serializer "PostSerializer"
  end
end
```

See [ActiveModelSerializers::Test::Serializer](../../lib/active_model_serializers/test/serializer.rb)
for more examples and documentation.

## Serialization against a schema

### Dependencies

To use the `assert_response_schema` you need to have the
[`json_schema`](https://github.com/brandur/json_schema) on your Gemfile. Please
add it to your Gemfile and run `$ bundle install`.

### Minitest test helpers

ActiveModelSerializers provides a `assert_response_schema` method to be used on your controller tests to
assert the response against a [JSON Schema](http://json-schema.org/). Let's take
a look in an example.

```ruby
class PostsController < ApplicationController
  def show
    @post = Post.find(params[:id])

    render json: @post
  end
end
```

To test the `posts#show` response of this controller we need to create a file
named `test/support/schemas/posts/show.json`. The helper uses a naming convention
to locate the file.

This file is a JSON Schema representation of our response.

```json
{
  "properties": {
    "title" : { "type" : "string" },
    "content" : { "type" : "string" }
  }
}
```

With all in place we can go to our test and use the helper.

```ruby
class PostsControllerTest < ActionController::TestCase
  test "should render right response" do
    get :index
    assert_response_schema
  end
end
```

#### Load a custom schema

If we need to use another schema, for example when we have a namespaced API that
shows the same response, we can pass the path of the schema.

```ruby
module V1
  class PostsController < ApplicationController
    def show
      @post = Post.find(params[:id])

      render json: @post
    end
  end
end
```

```ruby
class V1::PostsControllerTest < ActionController::TestCase
  test "should render right response" do
    get :index
    assert_response_schema('posts/show.json')
  end
end
```

#### Change the schema path

By default all schemas are created at `test/support/schemas`. If we are using
RSpec for example we can change this to `spec/support/schemas` defining the
default schema path in an initializer.

```ruby
ActiveModelSerializers.config.schema_path = 'spec/support/schemas'
```

#### Using with the Heroku’s JSON Schema-based tools

To use the test helper with the [prmd](https://github.com/interagent/prmd) and
[committee](https://github.com/interagent/committee).

We need to change the schema path to the recommended by prmd:

```ruby
ActiveModelSerializers.config.schema_path = 'docs/schema/schemata'
```

We also need to structure our schemata according to Heroku's conventions
(e.g. including
[required metadata](https://github.com/interagent/prmd/blob/master/docs/schemata.md#meta-data)
and [links](https://github.com/interagent/prmd/blob/master/docs/schemata.md#links).

#### JSON Pointers

If we plan to use [JSON
Pointers](http://spacetelescope.github.io/understanding-json-schema/UnderstandingJSONSchema.pdf) we need to define the `id` attribute on the schema. Example:

```js
// attributes.json

{
  "id": "file://attributes.json#",
  "properties": {
    "name" : { "type" : "string" },
    "description" : { "type" : "string" }
  }
}
```

```js
// show.json

{
  "properties": {
    "name": {
      "$ref": "file://attributes.json#/properties/name"
    },
    "description": {
      "$ref": "file://attributes.json#/properties/description"
    }
  }
}
```