|
1 | 1 | require 'spec_helper' |
2 | | -require 'rspec/expectations' |
3 | 2 |
|
4 | 3 | describe PostsController, type: :controller do |
5 | 4 | include_context 'JSON API headers' |
|
8 | 7 |
|
9 | 8 | let(:fields) { (PostResource.fetchable_fields - %i(id author)).map(&:to_s) } |
10 | 9 | let(:relationships) { %w(author) } |
11 | | - let(:post) { Post.first } |
| 10 | + let(:first_post) { Post.first } |
| 11 | + let(:user_id) { first_post.user_id } |
| 12 | + |
| 13 | + let(:attributes) do |
| 14 | + { title: 'Lorem ipsum', body: 'Lorem ipsum dolor sit amet.' } |
| 15 | + end |
| 16 | + |
| 17 | + let(:author_params) do |
| 18 | + { data: { type: 'users', id: user_id } } |
| 19 | + end |
| 20 | + |
| 21 | + let(:post_params) do |
| 22 | + { |
| 23 | + data: { type: 'posts', attributes: attributes }, |
| 24 | + relationships: { author: author_params } |
| 25 | + } |
| 26 | + end |
12 | 27 |
|
13 | 28 | describe '#index' do |
14 | 29 | context 'with ActiveRecord::Relation' do |
15 | 30 | it 'renders a collection of users' do |
16 | | - get :index, user_id: post.user_id |
| 31 | + get :index, user_id: user_id |
17 | 32 | expect(response).to have_http_status :ok |
18 | 33 | expect(response).to have_primary_data('posts') |
19 | 34 | expect(response).to have_data_attributes(fields) |
|
36 | 51 | describe '#show' do |
37 | 52 | context 'with ActiveRecord' do |
38 | 53 | it 'renders a single post' do |
39 | | - get :show, user_id: post.user_id, id: post.id |
| 54 | + get :show, user_id: user_id, id: first_post.id |
40 | 55 | expect(response).to have_http_status :ok |
41 | 56 | expect(response).to have_primary_data('posts') |
42 | 57 | expect(response).to have_data_attributes(fields) |
43 | 58 | expect(response).to have_relationships(relationships) |
44 | | - expect(data['attributes']['title']).to eq("Title for Post #{post.id}") |
| 59 | + expect(data['attributes']['title']).to eq("Title for Post #{first_post.id}") |
45 | 60 | end |
46 | 61 | end |
47 | 62 |
|
|
59 | 74 | context 'when resource was not found' do |
60 | 75 | context 'with conventional id' do |
61 | 76 | it 'renders a 404 response' do |
62 | | - get :show, user_id: post.user_id, id: 999 |
| 77 | + get :show, user_id: user_id, id: 999 |
63 | 78 | expect(response).to have_http_status :not_found |
64 | 79 | expect(error['title']).to eq('Record not found') |
65 | 80 | expect(error['detail']).to include('999') |
66 | | - expect(error['code']).to eq(404) |
| 81 | + expect(error['code']).to eq('404') |
67 | 82 | end |
68 | 83 | end |
69 | 84 |
|
70 | 85 | context 'with uuid' do |
71 | 86 | let(:uuid) { SecureRandom.uuid } |
72 | 87 |
|
73 | 88 | it 'renders a 404 response' do |
74 | | - get :show, user_id: post.user_id, id: uuid |
| 89 | + get :show, user_id: user_id, id: uuid |
75 | 90 | expect(response).to have_http_status :not_found |
76 | 91 | expect(error['title']).to eq('Record not found') |
77 | 92 | expect(error['detail']).to include(uuid) |
78 | | - expect(error['code']).to eq(404) |
| 93 | + expect(error['code']).to eq('404') |
79 | 94 | end |
80 | 95 | end |
81 | 96 |
|
82 | 97 | context 'with slug' do |
83 | 98 | let(:slug) { 'some-awesome-slug' } |
84 | 99 |
|
85 | 100 | it 'renders a 404 response' do |
86 | | - get :show, user_id: post.user_id, id: slug |
| 101 | + get :show, user_id: user_id, id: slug |
87 | 102 | expect(response).to have_http_status :not_found |
88 | 103 | expect(error['title']).to eq('Record not found') |
89 | 104 | expect(error['detail']).to include(slug) |
90 | | - expect(error['code']).to eq(404) |
| 105 | + expect(error['code']).to eq('404') |
91 | 106 | end |
92 | 107 | end |
93 | 108 | end |
94 | 109 | end |
| 110 | + |
| 111 | + describe '#create' do |
| 112 | + it 'creates a new post' do |
| 113 | + expect { post :create, post_params }.to change(Post, :count).by(1) |
| 114 | + expect(response).to have_http_status :created |
| 115 | + expect(response).to have_primary_data('posts') |
| 116 | + expect(response).to have_data_attributes(fields) |
| 117 | + expect(data['attributes']['title']).to eq(post_params[:data][:attributes][:title]) |
| 118 | + end |
| 119 | + |
| 120 | + context 'when validation fails' do |
| 121 | + it 'render a 422 response' do |
| 122 | + post_params[:data][:attributes].merge!(title: nil) |
| 123 | + |
| 124 | + expect { post :create, post_params }.to change(Post, :count).by(0) |
| 125 | + expect(response).to have_http_status :unprocessable_entity |
| 126 | + |
| 127 | + expect(errors[0]['id']).to eq('title') |
| 128 | + expect(errors[0]['title']).to eq('Title can\'t be blank') |
| 129 | + expect(errors[0]['code']).to eq('100') |
| 130 | + end |
| 131 | + end |
| 132 | + end |
95 | 133 | end |
0 commit comments