dry_crud 2.1.2 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/README.rdoc +1 -2
- data/VERSION +1 -1
- data/app/controllers/crud_controller.rb +96 -61
- data/app/controllers/dry_crud/generic_model.rb +1 -5
- data/app/controllers/dry_crud/rememberable.rb +1 -1
- data/app/controllers/dry_crud/render_callbacks.rb +2 -4
- data/app/controllers/list_controller.rb +4 -5
- data/app/helpers/dry_crud/form/builder.rb +4 -30
- data/app/helpers/format_helper.rb +2 -0
- data/app/helpers/utility_helper.rb +2 -3
- data/app/views/crud/show.json.jbuilder +1 -0
- data/app/views/layouts/_flash.html.erb +1 -1
- data/app/views/layouts/_flash.html.haml +1 -1
- data/app/views/list/index.json.jbuilder +4 -0
- data/config/locales/crud.de.yml +1 -1
- data/lib/generators/dry_crud/templates/spec/controllers/crud_test_models_controller_spec.rb +141 -139
- data/lib/generators/dry_crud/templates/spec/helpers/dry_crud/form/builder_spec.rb +47 -48
- data/lib/generators/dry_crud/templates/spec/helpers/dry_crud/table/builder_spec.rb +6 -6
- data/lib/generators/dry_crud/templates/spec/helpers/form_helper_spec.rb +66 -66
- data/lib/generators/dry_crud/templates/spec/helpers/format_helper_spec.rb +62 -61
- data/lib/generators/dry_crud/templates/spec/helpers/i18n_helper_spec.rb +13 -13
- data/lib/generators/dry_crud/templates/spec/helpers/table_helper_spec.rb +42 -38
- data/lib/generators/dry_crud/templates/spec/helpers/utility_helper_spec.rb +22 -22
- data/lib/generators/dry_crud/templates/spec/support/crud_controller_examples.rb +72 -72
- data/lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb +54 -45
- data/lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb +8 -11
- data/lib/generators/dry_crud/templates/test/helpers/dry_crud/form/builder_test.rb +1 -6
- data/lib/generators/dry_crud/templates/test/helpers/form_helper_test.rb +47 -46
- data/lib/generators/dry_crud/templates/test/helpers/format_helper_test.rb +1 -1
- data/lib/generators/dry_crud/templates/test/helpers/table_helper_test.rb +9 -3
- data/lib/generators/dry_crud/templates/test/support/crud_controller_test_helper.rb +2 -2
- data/lib/generators/dry_crud/templates/test/support/crud_test_helper.rb +1 -1
- data/lib/generators/dry_crud/templates/test/support/crud_test_model.rb +0 -4
- data/lib/generators/dry_crud/templates/test/support/crud_test_models_controller.rb +5 -10
- metadata +15 -18
- data/app/controllers/dry_crud/responder.rb +0 -34
@@ -1,5 +1,5 @@
|
|
1
1
|
# encoding: UTF-8
|
2
|
-
require '
|
2
|
+
require 'rails_helper'
|
3
3
|
|
4
4
|
describe UtilityHelper do
|
5
5
|
|
@@ -17,17 +17,17 @@ describe UtilityHelper do
|
|
17
17
|
let(:model) { crud_test_models(:AAAAA) }
|
18
18
|
|
19
19
|
it 'recognizes types' do
|
20
|
-
column_type(model, :name).
|
21
|
-
column_type(model, :children).
|
22
|
-
column_type(model, :companion_id).
|
23
|
-
column_type(model, :rating).
|
24
|
-
column_type(model, :income).
|
25
|
-
column_type(model, :birthdate).
|
26
|
-
column_type(model, :gets_up_at).
|
27
|
-
column_type(model, :last_seen).
|
28
|
-
column_type(model, :human).
|
29
|
-
column_type(model, :remarks).
|
30
|
-
column_type(model, :companion).
|
20
|
+
expect(column_type(model, :name)).to eq(:string)
|
21
|
+
expect(column_type(model, :children)).to eq(:integer)
|
22
|
+
expect(column_type(model, :companion_id)).to eq(:integer)
|
23
|
+
expect(column_type(model, :rating)).to eq(:float)
|
24
|
+
expect(column_type(model, :income)).to eq(:decimal)
|
25
|
+
expect(column_type(model, :birthdate)).to eq(:date)
|
26
|
+
expect(column_type(model, :gets_up_at)).to eq(:time)
|
27
|
+
expect(column_type(model, :last_seen)).to eq(:datetime)
|
28
|
+
expect(column_type(model, :human)).to eq(:boolean)
|
29
|
+
expect(column_type(model, :remarks)).to eq(:text)
|
30
|
+
expect(column_type(model, :companion)).to be_nil
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
@@ -35,39 +35,39 @@ describe UtilityHelper do
|
|
35
35
|
|
36
36
|
it 'escapes safe content' do
|
37
37
|
html = content_tag_nested(:div, %w(a b)) { |e| content_tag(:span, e) }
|
38
|
-
html.
|
39
|
-
html.
|
38
|
+
expect(html).to be_html_safe
|
39
|
+
expect(html).to eq('<div><span>a</span><span>b</span></div>')
|
40
40
|
end
|
41
41
|
|
42
42
|
it 'escapes unsafe content' do
|
43
43
|
html = content_tag_nested(:div, %w(a b)) { |e| "<#{e}>" }
|
44
|
-
html.
|
44
|
+
expect(html).to eq('<div><a><b></div>')
|
45
45
|
end
|
46
46
|
|
47
47
|
it 'simplys join without block' do
|
48
48
|
html = content_tag_nested(:div, %w(a b))
|
49
|
-
html.
|
49
|
+
expect(html).to eq('<div>ab</div>')
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
53
|
describe '#safe_join' do
|
54
54
|
it 'works as super without block' do
|
55
55
|
html = safe_join(['<a>', '<b>'.html_safe])
|
56
|
-
html.
|
56
|
+
expect(html).to eq('<a><b>')
|
57
57
|
end
|
58
58
|
|
59
59
|
it 'collects contents for array' do
|
60
60
|
html = safe_join(%w(a b)) { |e| content_tag(:span, e) }
|
61
|
-
html.
|
61
|
+
expect(html).to eq('<span>a</span><span>b</span>')
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
65
|
describe '#default_crud_attrs' do
|
66
66
|
it 'do not contain id and password' do
|
67
|
-
default_crud_attrs.
|
68
|
-
|
69
|
-
|
70
|
-
|
67
|
+
expect(default_crud_attrs).to eq([
|
68
|
+
:name, :email, :whatever, :children, :companion_id, :rating, :income,
|
69
|
+
:birthdate, :gets_up_at, :last_seen, :human, :remarks,
|
70
|
+
:created_at, :updated_at])
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
@@ -3,8 +3,8 @@ require 'support/crud_controller_test_helper'
|
|
3
3
|
|
4
4
|
RSpec.configure do |c|
|
5
5
|
c.before failing: true do
|
6
|
-
model_class.
|
7
|
-
model_class.
|
6
|
+
allow_any_instance_of(model_class).to receive(:save).and_return(false)
|
7
|
+
allow_any_instance_of(model_class).to receive(:destroy).and_return(false)
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
@@ -36,7 +36,7 @@ shared_examples 'crud controller' do |options|
|
|
36
36
|
end
|
37
37
|
|
38
38
|
before do
|
39
|
-
m =
|
39
|
+
m = RSpec.current_example.metadata
|
40
40
|
if m[:perform_request] != false && m[:action] && m[:method]
|
41
41
|
perform_combined_request
|
42
42
|
end
|
@@ -52,9 +52,9 @@ shared_examples 'crud controller' do |options|
|
|
52
52
|
context 'plain',
|
53
53
|
unless: skip?(options, %w(index html plain)),
|
54
54
|
combine: 'ihp' do
|
55
|
-
|
56
|
-
|
57
|
-
|
55
|
+
it_is_expected_to_respond
|
56
|
+
it_is_expected_to_assign_entries
|
57
|
+
it_is_expected_to_render
|
58
58
|
end
|
59
59
|
|
60
60
|
context 'search',
|
@@ -63,11 +63,11 @@ shared_examples 'crud controller' do |options|
|
|
63
63
|
combine: 'ihse' do
|
64
64
|
let(:params) { { q: search_value } }
|
65
65
|
|
66
|
-
|
66
|
+
it_is_expected_to_respond
|
67
67
|
|
68
68
|
context 'entries' do
|
69
69
|
subject { entries }
|
70
|
-
it {
|
70
|
+
it { is_expected.to include(test_entry) }
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
@@ -78,11 +78,11 @@ shared_examples 'crud controller' do |options|
|
|
78
78
|
combine: 'ihso' do
|
79
79
|
let(:params) { { sort: sort_column, sort_dir: 'asc' } }
|
80
80
|
|
81
|
-
|
81
|
+
it_is_expected_to_respond
|
82
82
|
|
83
|
-
it '
|
83
|
+
it 'has sorted entries' do
|
84
84
|
sorted = entries.sort_by(&(sort_column.to_sym))
|
85
|
-
entries.
|
85
|
+
expect(entries).to eq(sorted)
|
86
86
|
end
|
87
87
|
end
|
88
88
|
|
@@ -91,11 +91,11 @@ shared_examples 'crud controller' do |options|
|
|
91
91
|
combine: 'ihsd' do
|
92
92
|
let(:params) { { sort: sort_column, sort_dir: 'desc' } }
|
93
93
|
|
94
|
-
|
94
|
+
it_is_expected_to_respond
|
95
95
|
|
96
|
-
it '
|
96
|
+
it 'has sorted entries' do
|
97
97
|
sorted = entries.sort_by(&(sort_column.to_sym))
|
98
|
-
entries.
|
98
|
+
expect(entries).to eq(sorted.reverse)
|
99
99
|
end
|
100
100
|
end
|
101
101
|
end
|
@@ -105,9 +105,9 @@ shared_examples 'crud controller' do |options|
|
|
105
105
|
format: :json,
|
106
106
|
unless: skip?(options, %w(index json)),
|
107
107
|
combine: 'ij' do
|
108
|
-
|
109
|
-
|
110
|
-
it { response.body.
|
108
|
+
it_is_expected_to_respond
|
109
|
+
it_is_expected_to_assign_entries
|
110
|
+
it { expect(response.body).to start_with('[{') }
|
111
111
|
end
|
112
112
|
end
|
113
113
|
|
@@ -122,9 +122,9 @@ shared_examples 'crud controller' do |options|
|
|
122
122
|
context 'plain',
|
123
123
|
unless: skip?(options, %w(show html plain)),
|
124
124
|
combine: 'sh' do
|
125
|
-
|
126
|
-
|
127
|
-
|
125
|
+
it_is_expected_to_respond
|
126
|
+
it_is_expected_to_assign_entry
|
127
|
+
it_is_expected_to_render
|
128
128
|
end
|
129
129
|
|
130
130
|
context 'with non-existing id',
|
@@ -132,7 +132,7 @@ shared_examples 'crud controller' do |options|
|
|
132
132
|
%w(show html with_non_existing_id)) do
|
133
133
|
let(:params) { { id: 9999 } }
|
134
134
|
|
135
|
-
it '
|
135
|
+
it 'raises RecordNotFound', perform_request: false do
|
136
136
|
expect { perform_request }
|
137
137
|
.to raise_error(ActiveRecord::RecordNotFound)
|
138
138
|
end
|
@@ -143,9 +143,9 @@ shared_examples 'crud controller' do |options|
|
|
143
143
|
format: :json,
|
144
144
|
unless: skip?(options, %w(show json)),
|
145
145
|
combine: 'sj' do
|
146
|
-
|
147
|
-
|
148
|
-
|
146
|
+
it_is_expected_to_respond
|
147
|
+
it_is_expected_to_assign_entry
|
148
|
+
it_is_expected_to_render_json
|
149
149
|
end
|
150
150
|
end
|
151
151
|
|
@@ -154,15 +154,15 @@ shared_examples 'crud controller' do |options|
|
|
154
154
|
context 'plain',
|
155
155
|
unless: skip?(options, %w(new plain)),
|
156
156
|
combine: 'new' do
|
157
|
-
|
158
|
-
|
159
|
-
|
157
|
+
it_is_expected_to_respond
|
158
|
+
it_is_expected_to_render
|
159
|
+
it_is_expected_to_persist_entry(false)
|
160
160
|
end
|
161
161
|
|
162
162
|
context 'with params',
|
163
163
|
unless: skip?(options, %w(new with_params)) do
|
164
164
|
let(:params) { { model_identifier => new_entry_attrs } }
|
165
|
-
|
165
|
+
it_is_expected_to_set_attrs(:new)
|
166
166
|
end
|
167
167
|
end
|
168
168
|
|
@@ -170,55 +170,55 @@ shared_examples 'crud controller' do |options|
|
|
170
170
|
unless: skip?(options, %w(create)) do
|
171
171
|
let(:params) { { model_identifier => new_entry_attrs } }
|
172
172
|
|
173
|
-
it '
|
173
|
+
it 'adds entry to database', perform_request: false do
|
174
174
|
expect { perform_request }.to change { model_class.count }.by(1)
|
175
175
|
end
|
176
176
|
|
177
177
|
context 'html',
|
178
178
|
format: :html,
|
179
179
|
unless: skip?(options, %w(create html)) do
|
180
|
-
|
180
|
+
it_is_expected_to_persist_entry # cannot combine this
|
181
181
|
|
182
182
|
context 'with valid params',
|
183
183
|
unless: skip?(options, %w(create html valid)),
|
184
184
|
combine: 'chv' do
|
185
|
-
|
186
|
-
|
187
|
-
|
185
|
+
it_is_expected_to_redirect_to_show
|
186
|
+
it_is_expected_to_set_attrs(:new)
|
187
|
+
it_is_expected_to_have_flash(:notice)
|
188
188
|
end
|
189
189
|
|
190
190
|
context 'with invalid params',
|
191
191
|
failing: true,
|
192
192
|
unless: skip?(options, %w(create html invalid)),
|
193
193
|
combine: 'chi' do
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
194
|
+
it_is_expected_to_render('new')
|
195
|
+
it_is_expected_to_persist_entry(false)
|
196
|
+
it_is_expected_to_set_attrs(:new)
|
197
|
+
it_is_expected_to_not_have_flash(:notice)
|
198
198
|
end
|
199
199
|
end
|
200
200
|
|
201
201
|
context 'json',
|
202
202
|
format: :json,
|
203
203
|
unless: skip?(options, %w(create json)) do
|
204
|
-
|
204
|
+
it_is_expected_to_persist_entry # cannot combine this
|
205
205
|
|
206
206
|
context 'with valid params',
|
207
207
|
unless: skip?(options, %w(create json valid)),
|
208
208
|
combine: 'cjv' do
|
209
|
-
|
210
|
-
|
211
|
-
|
209
|
+
it_is_expected_to_respond(201)
|
210
|
+
it_is_expected_to_set_attrs(:new)
|
211
|
+
it_is_expected_to_render_json
|
212
212
|
end
|
213
213
|
|
214
214
|
context 'with invalid params',
|
215
215
|
failing: true,
|
216
216
|
unless: skip?(options, %w(create json invalid)),
|
217
217
|
combine: 'cji' do
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
218
|
+
it_is_expected_to_respond(422)
|
219
|
+
it_is_expected_to_set_attrs(:new)
|
220
|
+
it_is_expected_to_render_json
|
221
|
+
it_is_expected_to_persist_entry(false)
|
222
222
|
end
|
223
223
|
end
|
224
224
|
end
|
@@ -227,9 +227,9 @@ shared_examples 'crud controller' do |options|
|
|
227
227
|
id: true,
|
228
228
|
unless: skip?(options, %w(edit)),
|
229
229
|
combine: 'edit' do
|
230
|
-
|
231
|
-
|
232
|
-
|
230
|
+
it_is_expected_to_respond
|
231
|
+
it_is_expected_to_render
|
232
|
+
it_is_expected_to_assign_entry
|
233
233
|
end
|
234
234
|
|
235
235
|
describe_action :put, :update,
|
@@ -237,7 +237,7 @@ shared_examples 'crud controller' do |options|
|
|
237
237
|
unless: skip?(options, %w(update)) do
|
238
238
|
let(:params) { { model_identifier => edit_entry_attrs } }
|
239
239
|
|
240
|
-
it '
|
240
|
+
it 'updates entry in database', perform_request: false do
|
241
241
|
expect { perform_request }.to change { model_class.count }.by(0)
|
242
242
|
end
|
243
243
|
|
@@ -247,19 +247,19 @@ shared_examples 'crud controller' do |options|
|
|
247
247
|
context 'with valid params',
|
248
248
|
unless: skip?(options, %w(update html valid)),
|
249
249
|
combine: 'uhv' do
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
250
|
+
it_is_expected_to_set_attrs(:edit)
|
251
|
+
it_is_expected_to_redirect_to_show
|
252
|
+
it_is_expected_to_persist_entry
|
253
|
+
it_is_expected_to_have_flash(:notice)
|
254
254
|
end
|
255
255
|
|
256
256
|
context 'with invalid params',
|
257
257
|
failing: true,
|
258
258
|
unless: skip?(options, %w(update html invalid)),
|
259
259
|
combine: 'uhi' do
|
260
|
-
|
261
|
-
|
262
|
-
|
260
|
+
it_is_expected_to_render('edit')
|
261
|
+
it_is_expected_to_set_attrs(:edit)
|
262
|
+
it_is_expected_to_not_have_flash(:notice)
|
263
263
|
end
|
264
264
|
end
|
265
265
|
|
@@ -270,19 +270,19 @@ shared_examples 'crud controller' do |options|
|
|
270
270
|
context 'with valid params',
|
271
271
|
unless: skip?(options, %w(update json valid)),
|
272
272
|
combine: 'ujv' do
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
273
|
+
it_is_expected_to_respond(200)
|
274
|
+
it_is_expected_to_set_attrs(:edit)
|
275
|
+
it_is_expected_to_render_json
|
276
|
+
it_is_expected_to_persist_entry
|
277
277
|
end
|
278
278
|
|
279
279
|
context 'with invalid params',
|
280
280
|
failing: true,
|
281
281
|
unless: skip?(options, %w(update json invalid)),
|
282
282
|
combine: 'uji' do
|
283
|
-
|
284
|
-
|
285
|
-
|
283
|
+
it_is_expected_to_respond(422)
|
284
|
+
it_is_expected_to_set_attrs(:edit)
|
285
|
+
it_is_expected_to_render_json
|
286
286
|
end
|
287
287
|
end
|
288
288
|
end
|
@@ -291,7 +291,7 @@ shared_examples 'crud controller' do |options|
|
|
291
291
|
id: true,
|
292
292
|
unless: skip?(options, %w(destroy)) do
|
293
293
|
|
294
|
-
it '
|
294
|
+
it 'removes entry from database', perform_request: false do
|
295
295
|
expect { perform_request }.to change { model_class.count }.by(-1)
|
296
296
|
end
|
297
297
|
|
@@ -300,13 +300,13 @@ shared_examples 'crud controller' do |options|
|
|
300
300
|
unless: skip?(options, %w(destroy html)) do
|
301
301
|
|
302
302
|
context 'successfull', combine: 'dhs' do
|
303
|
-
|
304
|
-
|
303
|
+
it_is_expected_to_redirect_to_index
|
304
|
+
it_is_expected_to_have_flash(:notice)
|
305
305
|
end
|
306
306
|
|
307
307
|
context 'with failure', failing: true, combine: 'dhf' do
|
308
|
-
|
309
|
-
|
308
|
+
it_is_expected_to_redirect_to_index
|
309
|
+
it_is_expected_to_have_flash(:alert)
|
310
310
|
end
|
311
311
|
end
|
312
312
|
|
@@ -315,13 +315,13 @@ shared_examples 'crud controller' do |options|
|
|
315
315
|
unless: skip?(options, %w(destroy json)) do
|
316
316
|
|
317
317
|
context 'successfull', combine: 'djs' do
|
318
|
-
|
319
|
-
it { response.body.
|
318
|
+
it_is_expected_to_respond(204)
|
319
|
+
it { expect(response.body).to be_blank }
|
320
320
|
end
|
321
321
|
|
322
322
|
context 'with failure', failing: true, combine: 'djf' do
|
323
|
-
|
324
|
-
|
323
|
+
it_is_expected_to_respond(422)
|
324
|
+
it_is_expected_to_render_json
|
325
325
|
end
|
326
326
|
end
|
327
327
|
end
|
@@ -7,7 +7,7 @@ module CrudControllerTestHelper
|
|
7
7
|
|
8
8
|
# Performs a request based on the metadata of the action example under test.
|
9
9
|
def perform_request
|
10
|
-
m =
|
10
|
+
m = RSpec.current_example.metadata
|
11
11
|
example_params = respond_to?(:params) ? send(:params) : {}
|
12
12
|
params = scope_params.merge(format: m[:format])
|
13
13
|
params.merge!(id: test_entry.id) if m[:id]
|
@@ -22,29 +22,40 @@ module CrudControllerTestHelper
|
|
22
22
|
# If a combine key is given in metadata, only the first request for all
|
23
23
|
# examples with the same key will be performed.
|
24
24
|
def perform_combined_request
|
25
|
-
stack =
|
25
|
+
stack = RSpec.current_example.metadata[:combine]
|
26
26
|
if stack
|
27
27
|
@@current_stack ||= nil
|
28
28
|
if stack == @@current_stack &&
|
29
29
|
described_class == @@current_controller.class
|
30
|
-
|
31
|
-
@_templates = @templates = @@current_templates
|
32
|
-
@controller = @@current_controller
|
33
|
-
@request = @@current_request
|
30
|
+
restore_request
|
34
31
|
else
|
35
32
|
perform_request
|
36
|
-
|
37
33
|
@@current_stack = stack
|
38
|
-
|
39
|
-
@@current_request = @request
|
40
|
-
@@current_controller = @controller
|
41
|
-
@@current_templates = @_templates || @templates
|
34
|
+
remember_request
|
42
35
|
end
|
43
36
|
else
|
44
37
|
perform_request
|
45
38
|
end
|
46
39
|
end
|
47
40
|
|
41
|
+
def remember_request
|
42
|
+
@@current_response = @response
|
43
|
+
@@current_request = @request
|
44
|
+
@@current_controller = @controller
|
45
|
+
@@current_templates = @_templates || @templates
|
46
|
+
|
47
|
+
# treat in-memory entry as committed in order to
|
48
|
+
# avoid rollback of internal state.
|
49
|
+
entry.committed! if entry
|
50
|
+
end
|
51
|
+
|
52
|
+
def restore_request
|
53
|
+
@response = @@current_response
|
54
|
+
@_templates = @templates = @@current_templates
|
55
|
+
@controller = @@current_controller
|
56
|
+
@request = @@current_request
|
57
|
+
end
|
58
|
+
|
48
59
|
# The params defining the nesting of the test entry.
|
49
60
|
def scope_params
|
50
61
|
params = {}
|
@@ -88,92 +99,90 @@ module CrudControllerTestHelper
|
|
88
99
|
end
|
89
100
|
|
90
101
|
# Test the response status, default 200.
|
91
|
-
def
|
92
|
-
it { response.status.
|
102
|
+
def it_is_expected_to_respond(status = 200)
|
103
|
+
it { expect(response.status).to eq(status) }
|
93
104
|
end
|
94
105
|
|
95
106
|
# Test that entries are assigned.
|
96
|
-
def
|
97
|
-
it '
|
98
|
-
entries.
|
107
|
+
def it_is_expected_to_assign_entries
|
108
|
+
it 'assigns entries' do
|
109
|
+
expect(entries).to be_present
|
99
110
|
end
|
100
111
|
end
|
101
112
|
|
102
113
|
# Test that entry is assigned.
|
103
|
-
def
|
104
|
-
it '
|
105
|
-
entry.
|
114
|
+
def it_is_expected_to_assign_entry
|
115
|
+
it 'assigns entry' do
|
116
|
+
expect(entry).to eq(test_entry)
|
106
117
|
end
|
107
118
|
end
|
108
119
|
|
109
120
|
# Test that the given template or the main template of the action under
|
110
121
|
# test is rendered.
|
111
|
-
def
|
112
|
-
it
|
122
|
+
def it_is_expected_to_render(template = nil)
|
123
|
+
it do
|
124
|
+
template ||= RSpec.current_example.metadata[:action]
|
125
|
+
is_expected.to render_template(template)
|
126
|
+
end
|
113
127
|
end
|
114
128
|
|
115
129
|
# Test that a json response is rendered.
|
116
|
-
def
|
117
|
-
it { response.body.
|
118
|
-
end
|
119
|
-
|
120
|
-
# Test that a json response containing the error is rendered.
|
121
|
-
def it_should_render_error_json
|
122
|
-
it { response.body.should match(/"errors":\{/) }
|
130
|
+
def it_is_expected_to_render_json
|
131
|
+
it { expect(response.body).to start_with('{') }
|
123
132
|
end
|
124
133
|
|
125
134
|
# Test that test_entry_attrs are set on entry.
|
126
|
-
def
|
127
|
-
it '
|
135
|
+
def it_is_expected_to_set_attrs(action = nil)
|
136
|
+
it 'sets params as entry attributes' do
|
128
137
|
attrs = send("#{action}_entry_attrs")
|
129
138
|
actual = {}
|
130
139
|
attrs.keys.each do |key|
|
131
140
|
actual[key] = entry.attributes[key.to_s]
|
132
141
|
end
|
133
|
-
actual.
|
142
|
+
expect(actual).to eq(attrs)
|
134
143
|
end
|
135
144
|
end
|
136
145
|
|
137
146
|
# Test that the response redirects to the index action.
|
138
|
-
def
|
147
|
+
def it_is_expected_to_redirect_to_index
|
139
148
|
it do
|
140
|
-
|
141
|
-
|
149
|
+
is_expected.to redirect_to scope_params.merge(action: 'index',
|
150
|
+
returning: true)
|
142
151
|
end
|
143
152
|
end
|
144
153
|
|
145
154
|
# Test that the response redirects to the show action of the current entry.
|
146
|
-
def
|
155
|
+
def it_is_expected_to_redirect_to_show
|
147
156
|
it do
|
148
|
-
|
149
|
-
|
157
|
+
is_expected.to redirect_to scope_params.merge(action: 'show',
|
158
|
+
id: entry.id)
|
150
159
|
end
|
151
160
|
end
|
152
161
|
|
153
162
|
# Test that the given flash type is present.
|
154
|
-
def
|
163
|
+
def it_is_expected_to_have_flash(type, message = nil)
|
155
164
|
it "flash(#{type}) is set" do
|
156
|
-
flash[type].
|
165
|
+
expect(flash[type]).to(message ? match(message) : be_present)
|
157
166
|
end
|
158
167
|
end
|
159
168
|
|
160
169
|
# Test that not flash of the given type is present.
|
161
|
-
def
|
170
|
+
def it_is_expected_to_not_have_flash(type)
|
162
171
|
it "flash(#{type}) is nil" do
|
163
|
-
flash[type].
|
172
|
+
expect(flash[type]).to be_blank
|
164
173
|
end
|
165
174
|
end
|
166
175
|
|
167
176
|
# Test that the current entry is persistend and valid, or not.
|
168
|
-
def
|
177
|
+
def it_is_expected_to_persist_entry(bool = true)
|
169
178
|
context 'entry' do
|
170
179
|
subject { entry }
|
171
180
|
|
172
181
|
if bool
|
173
|
-
it {
|
174
|
-
it {
|
182
|
+
it { is_expected.not_to be_new_record }
|
183
|
+
it { is_expected.to be_valid }
|
175
184
|
else
|
176
|
-
it {
|
185
|
+
it { is_expected.to be_new_record }
|
177
186
|
end
|
178
187
|
end
|
179
188
|
end
|