Class: CrudTestModelsControllerTest
- Inherits:
-
ActionController::TestCase
- Object
- ActionController::TestCase
- CrudTestModelsControllerTest
show all
- Includes:
- CrudControllerTestHelper, CrudTestHelper
- Defined in:
- lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb
Overview
Tests all actions of the CrudController based on a dummy model (CrudTestModel). This is useful to test the general behavior of CrudController.
Instance Attribute Summary collapse
Instance Method Summary
collapse
#action_name, #controller_name, #h, #model_class, #params, #path_args, #sortable?
#ivar, #perform_combined_request, #perform_request, #remember_request, #restore_request, #scope_params, #test_create_json, #test_destroy_json, #test_index_json, #test_index_sort_asc, #test_index_sort_desc, #test_show, #test_show_json, #test_show_with_non_existing_id_raises_record_not_found, #test_update_json
Instance Attribute Details
#models ⇒ Object
Returns the value of attribute models.
14
15
16
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 14
def models
@models
end
|
Instance Method Details
#test_create ⇒ Object
134
135
136
137
138
139
140
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 134
def test_create
super
assert_match(/model got created/, flash[:notice])
assert flash[:alert].blank?
assert_equal %i[before_create before_save after_save after_create],
@controller.called_callbacks
end
|
#test_create_with_before_callback ⇒ Object
167
168
169
170
171
172
173
174
175
176
177
178
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 167
def test_create_with_before_callback
assert_no_difference('CrudTestModel.count') do
post :create,
params: { crud_test_model: { name: 'illegal', children: 2 } }
end
assert_response :success
assert entry.new_record?
assert flash[:alert].present?
assert_equal 'illegal', entry.name
assert_equal %i[before_render_new before_render_form],
@controller.called_callbacks
end
|
#test_create_with_before_callback_redirect ⇒ Object
180
181
182
183
184
185
186
187
188
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 180
def test_create_with_before_callback_redirect
@controller.should_redirect = true
assert_no_difference('CrudTestModel.count') do
post :create,
params: { crud_test_model: { name: 'illegal', children: 2 } }
end
assert_redirected_to action: 'index'
assert_nil @controller.called_callbacks
end
|
#test_create_with_failure ⇒ Object
196
197
198
199
200
201
202
203
204
205
206
207
208
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 196
def test_create_with_failure
assert_no_difference('CrudTestModel.count') do
post :create, params: { crud_test_model: { children: 2 } }
end
assert_response :success
assert entry.new_record?
assert flash[:notice].blank?, flash[:notice].to_s
assert flash[:alert].blank?, flash[:alert].to_s
assert entry.name.blank?
assert_equal %i[before_create before_save
before_render_new before_render_form],
@controller.called_callbacks
end
|
#test_create_with_failure_json ⇒ Object
210
211
212
213
214
215
216
217
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 210
def test_create_with_failure_json
assert_no_difference('CrudTestModel.count') do
post :create, params: { crud_test_model: { children: 2 }, format: 'json' }
end
assert_response :unprocessable_entity
assert entry.new_record?
assert_equal %i[before_create before_save], @controller.called_callbacks
end
|
#test_destroy ⇒ Object
158
159
160
161
162
163
164
165
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 158
def test_destroy
super
assert_equal %i[before_destroy after_destroy],
@controller.called_callbacks
assert_equal I18n.t('crud.destroy.flash.success',
model: 'Crud Test Model <i>AAAAA</i>'),
flash[:notice]
end
|
#test_destroy_failure ⇒ Object
263
264
265
266
267
268
269
270
271
272
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 263
def test_destroy_failure
assert_no_difference("#{model_class.name}.count") do
@request.env['HTTP_REFERER'] =
crud_test_model_url(crud_test_models(:BBBBB))
delete :destroy, params: test_params(id: crud_test_models(:BBBBB).id)
end
assert_redirected_to_show(entry)
assert_match(/companion/, flash[:alert])
assert flash[:notice].blank?
end
|
#test_destroy_failure_callback ⇒ Object
274
275
276
277
278
279
280
281
282
283
284
285
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 274
def test_destroy_failure_callback
e = crud_test_models(:AAAAA)
e.update_attribute(:name, 'illegal')
assert_no_difference("#{model_class.name}.count") do
delete :destroy, params: test_params(id: e.id)
end
assert_redirected_to_index
assert_match(/illegal name/, flash[:alert])
assert flash[:notice].blank?
end
|
#test_destroy_failure_json ⇒ Object
287
288
289
290
291
292
293
294
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 287
def test_destroy_failure_json
assert_no_difference("#{model_class.name}.count") do
delete :destroy, params: test_params(id: crud_test_models(:BBBBB).id,
format: 'json')
end
assert_response :unprocessable_entity
assert flash[:notice].blank?
end
|
#test_edit ⇒ Object
142
143
144
145
146
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 142
def test_edit
super
assert_equal %i[before_render_edit before_render_form],
@controller.called_callbacks
end
|
#test_index ⇒ Object
32
33
34
35
36
37
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 32
def test_index
super
assert_equal 6, entries.size
assert_equal entries.sort_by(&:name), entries
assert_equal({}, session[:list_params])
end
|
#test_index_js ⇒ Object
39
40
41
42
43
44
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 39
def test_index_js
get :index, params: test_params, xhr: true
assert_response :success
assert_equal 'index js', @response.body
assert entries.present?
end
|
#test_index_returning ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 107
def test_index_returning
session[:list_params] = {}
session[:list_params]['/crud_test_models'] = { 'q' => 'DDD',
'sort' => 'chatty',
'sort_dir' => 'desc' }
get :index, params: { returning: true }
assert_response :success
assert entries.present?
assert_equal 3, entries.size
assert_equal %w[BBBBB DDDDD CCCCC], entries.map(&:name)
assert_equal 'DDD', @controller.params[:q]
assert_equal 'chatty', @controller.params[:sort]
assert_equal 'desc', @controller.params[:sort_dir]
end
|
#test_index_search ⇒ Object
46
47
48
49
50
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 46
def test_index_search
super
assert_equal 1, entries.size
assert_equal({ 'q' => 'AAAA' }, session[:list_params]['/crud_test_models'])
end
|
#test_index_search_with_custom_options ⇒ Object
60
61
62
63
64
65
66
67
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 60
def test_index_search_with_custom_options
get :index, params: { q: 'DDD', filter: true }
assert_response :success
assert entries.present?
assert_equal 1, entries.size
assert_equal [CrudTestModel.find_by(name: 'BBBBB')], entries
assert_equal({ 'q' => 'DDD' }, session[:list_params]['/crud_test_models'])
end
|
#test_index_with_custom_options ⇒ Object
52
53
54
55
56
57
58
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 52
def test_index_with_custom_options
get :index, params: { filter: true }
assert_response :success
assert entries.present?
assert_equal 2, entries.size
assert_equal entries.sort_by(&:children).reverse, entries
end
|
#test_models_label ⇒ Object
296
297
298
299
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 296
def test_models_label
assert_equal 'Crud Test Models', @controller.models_label
assert_equal 'Crud Test Model', @controller.models_label(plural: false)
end
|
#test_new ⇒ Object
122
123
124
125
126
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 122
def test_new
super
assert_equal %i[before_render_new before_render_form],
@controller.called_callbacks
end
|
#test_new_with_before_render_callback_redirect_does_not_set_companions ⇒ Object
190
191
192
193
194
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 190
def test_new_with_before_render_callback_redirect_does_not_set_companions
@controller.should_redirect = true
get :new
assert_redirected_to action: 'index'
end
|
#test_setup ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 20
def test_setup
assert_equal 6, CrudTestModel.count
assert_equal CrudTestModelsController, @controller.class
assert_recognizes({ controller: 'crud_test_models',
action: 'index' },
'/crud_test_models')
assert_recognizes({ controller: 'crud_test_models',
action: 'show',
id: '1' },
'/crud_test_models/1')
end
|
#test_show_with_custom ⇒ Object
128
129
130
131
132
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 128
def test_show_with_custom
get :show, params: test_params(id: crud_test_models(:BBBBB).id)
assert_response :success
assert_equal 'custom html', @response.body
end
|
#test_sort_given_column ⇒ Object
69
70
71
72
73
74
75
76
77
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 69
def test_sort_given_column
get :index, params: { sort: 'children', sort_dir: 'asc' }
assert_response :success
assert entries.present?
assert_equal 6, entries.size
assert_equal CrudTestModel.all.sort_by(&:children), entries
assert_equal({ 'sort' => 'children', 'sort_dir' => 'asc' },
session[:list_params]['/crud_test_models'])
end
|
#test_sort_virtual_column ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 79
def test_sort_virtual_column
get :index, params: { sort: 'chatty', sort_dir: 'desc' }
assert_response :success
assert entries.present?
assert_equal 6, entries.size
assert_equal({ 'sort' => 'chatty', 'sort_dir' => 'desc' },
session[:list_params]['/crud_test_models'])
names = entries.map(&:name)
assert names.index('BBBBB') < names.index('AAAAA')
assert names.index('BBBBB') < names.index('DDDDD')
assert names.index('EEEEE') < names.index('AAAAA')
assert names.index('EEEEE') < names.index('DDDDD')
assert names.index('AAAAA') < names.index('CCCCC')
assert names.index('DDDDD') < names.index('CCCCC')
end
|
#test_sort_with_search ⇒ Object
97
98
99
100
101
102
103
104
105
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 97
def test_sort_with_search
get :index, params: { q: 'DDD', sort: 'chatty', sort_dir: 'asc' }
assert_response :success
assert entries.present?
assert_equal 3, entries.size
assert_equal %w[CCCCC DDDDD BBBBB], entries.map(&:name)
assert_equal({ 'sort' => 'chatty', 'sort_dir' => 'asc', 'q' => 'DDD' },
session[:list_params]['/crud_test_models'])
end
|
#test_update ⇒ Object
148
149
150
151
152
153
154
155
156
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 148
def test_update
super
assert_equal I18n.t('crud.update.flash.success',
model: 'Crud Test Model <i>foo</i>'),
flash[:notice]
assert flash[:alert].blank?
assert_equal %i[before_update before_save after_save after_update],
@controller.called_callbacks
end
|
#test_update_with_failure ⇒ Object
219
220
221
222
223
224
225
226
227
228
229
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 219
def test_update_with_failure
put :update, params: { id: test_entry.id, crud_test_model: { rating: 20 } }
assert_response :success
assert entry.changed?
assert flash[:notice].blank?
assert flash[:alert].blank?
assert_equal 20, entry.rating
assert_equal %i[before_update before_save
before_render_edit before_render_form],
@controller.called_callbacks
end
|
#test_update_with_failure_does_not_update_many_relations ⇒ Object
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 231
def test_update_with_failure_does_not_update_many_relations
put :update,
params: {
id: test_entry.id,
crud_test_model: {
rating: 20,
other_ids: [OtherCrudTestModel.first.id]
}
}
assert_response :success
assert entry.changed?
assert flash[:notice].blank?
assert flash[:alert].blank?
assert_equal 20, entry.rating
assert_equal %i[before_update before_save
before_render_edit before_render_form],
@controller.called_callbacks
assert_equal [], test_entry.reload.other_ids
end
|
#test_update_with_failure_json ⇒ Object
251
252
253
254
255
256
257
258
259
260
261
|
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 251
def test_update_with_failure_json
put :update,
params: { id: test_entry.id,
crud_test_model: { rating: 20 },
format: 'json' }
assert_response :unprocessable_entity
assert entry.changed?
assert flash[:notice].blank?
assert_equal 20, entry.rating
assert_equal %i[before_update before_save], @controller.called_callbacks
end
|