-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathassert_payload_spec.rb
More file actions
286 lines (247 loc) · 8.27 KB
/
assert_payload_spec.rb
File metadata and controls
286 lines (247 loc) · 8.27 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
require 'spec_helper'
describe JsonapiSpecHelpers do
include JsonapiSpecHelpers
attr_accessor :response
let(:instance) { klass.new }
let(:json) do
{
'data' => {
'type' => 'posts',
'id' => '1',
'attributes' => {
'title' => 'post title',
'description' => 'post description',
'views' => 100,
'first_title_letter' => 'p'
}
}
}
end
after do
JsonapiSpecHelpers::Payload.registry = {}
end
describe '#assert_payload' do
before do
JsonapiSpecHelpers::Payload.register(:post) do
key(:title)
key(:description)
key(:first_title_letter) { |p| p.title[0] }
key(:views, Integer)
end
end
context 'when payload is valid' do
let(:post_record) do
attrs = {
id: 1,
title: 'post title',
description: 'post description',
views: 100
}
double(attrs).as_null_object
end
let(:some_let_var) { 'foo' }
it 'passes assertion' do
assert_payload(:post, post_record, json_item)
end
it 'has access to spec context' do
post_record.title = some_let_var
item = json_item
item['title'] = some_let_var
assert_payload(:post, post_record, item) do
key(:title, String) { some_let_var }
end
end
context 'when dasherized: true' do
before do
JsonapiSpecHelpers::Payload.register(:post_with_dasherized_attribute) do
key(:dasherized_attribute)
end
end
let(:dasherized_value) { 'some value' }
let(:post_record) do
attrs = {
id: 1,
dasherized_attribute: dasherized_value
}
double(attrs).as_null_object
end
let(:json) do
{
'data' => {
'type' => 'posts',
'id' => '1',
'attributes' => {
'dasherized-attribute' => dasherized_value
}
}
}
end
it 'passes assertion' do
assert_payload(:post_with_dasherized_attribute, post_record, json_item, dasherized: true)
end
end
context 'when json value matches payload value, but wrong type' do
before do
json['data']['attributes']['views'] = '100'
allow(post_record).to receive(:views) { '100' }
end
it 'does not pass assertion' do
expect {
assert_payload(:post, post_record, json_item)
}.to raise_error(
RSpec::Expectations::ExpectationNotMetError,
/Expected JSON payload key 'views' to have type Integer but was String/
)
end
end
context 'when a condition that matches multiple types' do
it 'passes assertion if one of the types passes' do
json['data']['attributes']['is_foo'] = false
allow(post_record).to receive(:is_foo) { false }
expect {
assert_payload(:post, post_record, json_item) do
key(:is_foo, [TrueClass, FalseClass])
end
}.to_not raise_error
json['data']['attributes']['is_foo'] = true
allow(post_record).to receive(:is_foo) { true }
expect {
assert_payload(:post, post_record, json_item) do
key(:is_foo, [TrueClass, FalseClass])
end
}.to_not raise_error
json['data']['attributes']['is_foo'] = 'true'
allow(post_record).to receive(:is_foo) { 'true' }
expect {
assert_payload(:post, post_record, json_item) do
key(:is_foo, [TrueClass, FalseClass])
end
}.to raise_error(RSpec::Expectations::ExpectationNotMetError)
end
end
context 'when json value does not match payload value' do
before do
allow(post_record).to receive(:title) { 'foo' }
end
it 'does not pass assertion' do
expect {
assert_payload(:post, post_record, json_item)
}.to raise_error(
RSpec::Expectations::ExpectationNotMetError,
/Expected JSON payload to have key 'title' == "foo" but was "post title"/
)
end
end
context 'when given timestamps! in payload' do
let(:timestamp) { Time.now.to_json }
before do
JsonapiSpecHelpers::Payload.register(:post_with_timestamps) do
key(:title)
key(:description)
key(:views)
timestamps!
end
json['data']['attributes'].delete('first_title_letter')
json['data']['attributes']['created_at'] = timestamp
json['data']['attributes']['updated_at'] = timestamp
end
it 'should assert on created_at/updated_at' do
expect(post_record).to receive(:created_at) { timestamp }
expect(post_record).to receive(:updated_at) { timestamp }
assert_payload(:post_with_timestamps, post_record, json_item)
end
end
context 'when json contains payload key, but it is nil' do
before do
json['data']['attributes'].merge!('title' => nil)
end
it 'does not pass assertion' do
expect {
assert_payload(:post, post_record, json_item)
}.to raise_error(
RSpec::Expectations::ExpectationNotMetError,
/but was nil/
)
end
context 'and allow_nil is true' do
before do
JsonapiSpecHelpers::Payload.register(:post_with_nil_title) do
key(:title, allow_nil: true)
key(:description)
key(:views)
key(:first_title_letter) { |p| p.title[0] }
end
end
it 'does not throw an error' do
expect {
assert_payload(:post_with_nil_title, post_record, json_item)
}.to_not raise_error
end
end
end
context 'when json does not contain payload key' do
before do
json['data']['attributes'].delete('title')
end
it 'does not pass assertion' do
expect {
assert_payload(:post, post_record, json_item)
}.to raise_error(
RSpec::Expectations::ExpectationNotMetError,
/Expected JSON payload to have key 'title' but was not present/
)
end
end
context 'when json contains extra keys' do
before do
json['data']['attributes']['foo'] = 'bar'
end
it 'does not pass assertion' do
expect {
assert_payload(:post, post_record, json_item)
}.to raise_error(
RSpec::Expectations::ExpectationNotMetError,
/JSON payload contained unexpected key 'foo'/
)
end
end
context 'when no payloads registered for this name' do
it 'raises an error' do
expect {
assert_payload(:asdf, post_record, json_item)
}.to raise_error(/No payloads registered for 'asdf'/)
end
end
context 'when asserting with customized block' do
before do
json['data']['attributes']['title'].upcase!
end
# Note this tests asserts errors are raised before and after to ensure
# the block customization won't screw up future tests.
it 'customizes assertions based on the block overrides' do
expect {
assert_payload(:post, post_record, json_item)
}.to raise_error(RSpec::Expectations::ExpectationNotMetError)
assert_payload(:post, post_record, json_item) do
key(:title) { |r| r.title.upcase }
end
expect {
assert_payload(:post, post_record, json_item)
}.to raise_error(RSpec::Expectations::ExpectationNotMetError)
end
context 'when no_key is specified' do
it 'asserts there is not a key present' do
expect {
assert_payload(:post, post_record, json_item) do
no_key(:title)
end
}.to raise_error(
RSpec::Expectations::ExpectationNotMetError,
/Expected JSON payload to NOT have key 'title' but was present/
)
end
end
end
end
end
end