Skip to content

Commit 0d51c35

Browse files
committed
DRY up some test examples
1 parent 13ca293 commit 0d51c35

File tree

6 files changed

+45
-84
lines changed

6 files changed

+45
-84
lines changed

Gemfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
source 'https://rubygems.org'
2-
3-
# Specify your gem's dependencies in jsonapi-utils.gemspec
42
gemspec

jsonapi-utils.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ Gem::Specification.new do |spec|
2424
spec.add_development_dependency 'bundler', '~> 1.10'
2525
spec.add_development_dependency 'rake', '~> 10.0'
2626
spec.add_development_dependency 'rspec-rails', '~> 3.1'
27+
spec.add_development_dependency 'smart_rspec', '~> 0.1.4'
2728
spec.add_development_dependency 'sqlite3'
2829
spec.add_development_dependency 'factory_girl', '~> 4.5'
2930
spec.add_development_dependency 'jsonapi-resources', '~> 0.7.0'
3031
spec.add_development_dependency 'rails', '~> 4.2'
31-
spec.add_development_dependency 'pry'
3232
end

spec/controllers/posts_controller_spec.rb

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'spec_helper'
2+
require 'rspec/expectations'
23

34
describe PostsController, type: :controller do
45
before(:all) { FactoryGirl.create_list(:post, 3) }
@@ -12,20 +13,20 @@
1213
it 'renders a collection of users' do
1314
get :index, user_id: post.user_id
1415
expect(response).to have_http_status :ok
15-
expect(has_valid_id_and_type_members?('posts')).to be_truthy
16-
expect(has_fetchable_fields?(fields)).to be_truthy
17-
expect(has_relationship_members?(relationships)).to be_truthy
18-
expect(record_count).to eq(100)
16+
expect(response).to have_primary_data('posts')
17+
expect(response).to have_data_attributes(fields)
18+
expect(response).to have_relationships(relationships)
19+
expect(response).to have_meta_record_count(100)
1920
end
2021
end
2122

2223
context 'with Hash' do
2324
it 'renders a collection of users' do
2425
get :index_with_hash
2526
expect(response).to have_http_status :ok
26-
expect(has_valid_id_and_type_members?('posts')).to be_truthy
27-
expect(has_fetchable_fields?(fields)).to be_truthy
28-
expect(has_relationship_members?(relationships)).to be_truthy
27+
expect(response).to have_primary_data('posts')
28+
expect(response).to have_data_attributes(fields)
29+
expect(response).to have_relationships(relationships)
2930
end
3031
end
3132
end
@@ -35,8 +36,9 @@
3536
it 'renders a single post' do
3637
get :show, user_id: post.user_id, id: post.id
3738
expect(response).to have_http_status :ok
38-
expect(has_valid_id_and_type_members?('posts')).to be_truthy
39-
expect(has_relationship_members?(relationships)).to be_truthy
39+
expect(response).to have_primary_data('posts')
40+
expect(response).to have_data_attributes(fields)
41+
expect(response).to have_relationships(relationships)
4042
expect(data['attributes']['title']).to eq("Title for Post #{post.id}")
4143
end
4244
end
@@ -45,8 +47,9 @@
4547
it 'renders a single post' do
4648
get :show_with_hash, id: 1
4749
expect(response).to have_http_status :ok
48-
expect(has_valid_id_and_type_members?('posts')).to be_truthy
49-
expect(has_relationship_members?(relationships)).to be_truthy
50+
expect(response).to have_primary_data('posts')
51+
expect(response).to have_data_attributes(fields)
52+
expect(response).to have_relationships(relationships)
5053
expect(data['attributes']['title']).to eq('Lorem ipsum')
5154
end
5255
end

spec/controllers/users_controller_spec.rb

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,28 @@
1212
it 'renders a collection of users' do
1313
get :index
1414
expect(response).to have_http_status :ok
15-
expect(has_valid_id_and_type_members?('users')).to be_truthy
16-
expect(has_fetchable_fields?(fields)).to be_truthy
17-
expect(has_relationship_members?(relationships)).to be_truthy
15+
expect(response).to have_primary_data('users')
16+
expect(response).to have_data_attributes(fields)
17+
expect(response).to have_relationships(relationships)
1818
end
1919

2020
context 'with "include"' do
2121
it 'returns only the required relationships in the "included" member' do
2222
get :index, include: :posts
2323
expect(response).to have_http_status :ok
24-
expect(has_valid_id_and_type_members?('users')).to be_truthy
25-
expect(has_included_relationships?(%w(posts))).to be_truthy
24+
expect(response).to have_primary_data('users')
25+
expect(response).to have_data_attributes(fields)
26+
expect(response).to have_relationships(relationships)
27+
expect(response).to have_included_relationships
2628
end
2729
end
2830

2931
context 'with "fields"' do
3032
it 'returns only the required fields in the "attributes" member' do
3133
get :index, fields: { users: :first_name }
3234
expect(response).to have_http_status :ok
33-
expect(has_valid_id_and_type_members?('users')).to be_truthy
34-
expect(has_fetchable_fields?(%w(first_name))).to be_truthy
35+
expect(response).to have_primary_data('users')
36+
expect(response).to have_data_attributes(%w(first_name))
3537
end
3638
end
3739

@@ -41,8 +43,8 @@
4143
it 'returns only results corresponding to the applied filter' do
4244
get :index, filter: { first_name: first_name }
4345
expect(response).to have_http_status :ok
44-
expect(has_valid_id_and_type_members?('users')).to be_truthy
45-
expect(record_count).to eq(1)
46+
expect(response).to have_primary_data('users')
47+
expect(response).to have_meta_record_count(1)
4648
expect(data[0]['attributes']['first_name']).to eq(first_name)
4749
end
4850
end
@@ -59,9 +61,9 @@
5961
get :index, page: { number: 1, size: 2 }
6062

6163
expect(response).to have_http_status :ok
62-
expect(has_valid_id_and_type_members?('users')).to be_truthy
64+
expect(response).to have_primary_data('users')
6365
expect(data.size).to eq(2)
64-
expect(record_count).to eq(3)
66+
expect(response).to have_meta_record_count(3)
6567

6668
expect(json['links']['first']).to be_present
6769
expect(json['links']['next']).to be_present
@@ -74,9 +76,9 @@
7476
get :index, page: { number: 2, size: 1 }
7577

7678
expect(response).to have_http_status :ok
77-
expect(has_valid_id_and_type_members?('users')).to be_truthy
79+
expect(response).to have_primary_data('users')
7880
expect(data.size).to eq(1)
79-
expect(record_count).to eq(3)
81+
expect(response).to have_meta_record_count(3)
8082

8183
expect(json['links']['first']).to be_present
8284
expect(json['links']['prev']).to be_present
@@ -90,9 +92,9 @@
9092
get :index, page: { number: 3, size: 1 }
9193

9294
expect(response).to have_http_status :ok
93-
expect(has_valid_id_and_type_members?('users')).to be_truthy
95+
expect(response).to have_primary_data('users')
9496
expect(data.size).to eq(1)
95-
expect(record_count).to eq(3)
97+
expect(response).to have_meta_record_count(3)
9698

9799
expect(json['links']['first']).to be_present
98100
expect(json['links']['prev']).to be_present
@@ -105,7 +107,7 @@
105107
get :index, page: { number: 1 }
106108
expect(response).to have_http_status :ok
107109
expect(data.size).to eq(JSONAPI.configuration.default_page_size)
108-
expect(record_count).to eq(3)
110+
expect(response).to have_meta_record_count(3)
109111
end
110112
end
111113
end
@@ -121,9 +123,9 @@
121123
get :index, page: { offset: 0, limit: 2 }
122124

123125
expect(response).to have_http_status :ok
124-
expect(has_valid_id_and_type_members?('users')).to be_truthy
126+
expect(response).to have_primary_data('users')
125127
expect(data.size).to eq(2)
126-
expect(record_count).to eq(3)
128+
expect(response).to have_meta_record_count(3)
127129

128130
expect(json['links']['first']).to be_present
129131
expect(json['links']['next']).to be_present
@@ -136,9 +138,9 @@
136138
get :index, page: { offset: 1, limit: 1 }
137139

138140
expect(response).to have_http_status :ok
139-
expect(has_valid_id_and_type_members?('users')).to be_truthy
141+
expect(response).to have_primary_data('users')
140142
expect(data.size).to eq(1)
141-
expect(record_count).to eq(3)
143+
expect(response).to have_meta_record_count(3)
142144

143145
expect(json['links']['first']).to be_present
144146
expect(json['links']['previous']).to be_present
@@ -152,9 +154,9 @@
152154
get :index, page: { offset: 2, limit: 1 }
153155

154156
expect(response).to have_http_status :ok
155-
expect(has_valid_id_and_type_members?('users')).to be_truthy
157+
expect(response).to have_primary_data('users')
156158
expect(data.size).to eq(1)
157-
expect(record_count).to eq(3)
159+
expect(response).to have_meta_record_count(3)
158160

159161
expect(json['links']['first']).to be_present
160162
expect(json['links']['previous']).to be_present
@@ -167,7 +169,7 @@
167169
get :index, page: { offset: 1 }
168170
expect(response).to have_http_status :ok
169171
expect(data.size).to eq(JSONAPI.configuration.default_page_size)
170-
expect(record_count).to eq(3)
172+
expect(response).to have_meta_record_count(3)
171173
end
172174
end
173175
end
@@ -182,7 +184,7 @@
182184
first_name2 = data[1]['attributes']['first_name']
183185

184186
expect(response).to have_http_status :ok
185-
expect(has_valid_id_and_type_members?('users')).to be_truthy
187+
expect(response).to have_primary_data('users')
186188
expect(first_name1).to be <= first_name2
187189
end
188190
end
@@ -196,7 +198,7 @@
196198
sorted = first_name1 > first_name2 || (first_name1 == first_name2 && last_name1 >= last_name2)
197199

198200
expect(response).to have_http_status :ok
199-
expect(has_valid_id_and_type_members?('users')).to be_truthy
201+
expect(response).to have_primary_data('users')
200202
expect(sorted).to be_truthy
201203
end
202204
end
@@ -209,8 +211,8 @@
209211
it 'renders a single user' do
210212
get :show, id: user.id
211213
expect(response).to have_http_status :ok
212-
expect(has_valid_id_and_type_members?('users')).to be_truthy
213-
expect(has_relationship_members?(relationships)).to be_truthy
214+
expect(response).to have_primary_data('users')
215+
expect(response).to have_data_attributes(fields)
214216
expect(data['attributes']['first_name']).to eq("User ##{user.id}")
215217
end
216218

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'rails/all'
22
require 'rails/test_help'
33
require 'rspec/rails'
4+
require 'smart_rspec'
45
require 'factory_girl'
56

67
require 'jsonapi-resources'

spec/support/helpers.rb

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,48 +21,5 @@ def collection
2121
def links
2222
@links ||= json['links']
2323
end
24-
25-
def included
26-
@included ||= json['included']
27-
end
28-
29-
def record_count
30-
@record_count ||= json['meta']['record_count']
31-
end
32-
33-
def has_fetchable_fields?(fields)
34-
collection.all? { |record| record['attributes'].keys == fields }
35-
end
36-
37-
def has_valid_id_and_type_members?(type)
38-
collection.all? { |record| record['id'].present? && record['type'] == type }
39-
end
40-
41-
def has_relationship_members?(relationships)
42-
collection.all? { |record| record['relationships'].keys == relationships }
43-
end
44-
45-
def has_included_relationships?(relationships)
46-
collection.all? do |record|
47-
record['relationships'].all? do |(key, relation)|
48-
return true if relation['data'].blank?
49-
relationship_ids = relation['data'].map { |e| e['id'] }
50-
(relationship_ids - included_ids[key]).empty?
51-
end
52-
end
53-
end
54-
55-
def included_ids
56-
return false unless included.present?
57-
@included_ids ||= included.reduce({}) do |sum, record|
58-
sum.tap do |hash|
59-
if hash[record['type']].blank?
60-
hash[record['type']] = Array(record['id'])
61-
else
62-
hash[record['type']].push(record['id'])
63-
end
64-
end
65-
end
66-
end
6724
end
6825
end

0 commit comments

Comments
 (0)