Skip to content

Commit 1d5e89a

Browse files
committed
Standardize param input in conference form test.
This makes it easier to see what the current test is actually testing. Yes, the tests still need a lot of work.
1 parent a4ae667 commit 1d5e89a

File tree

1 file changed

+54
-169
lines changed

1 file changed

+54
-169
lines changed

test/forms/conference_form_test.rb

Lines changed: 54 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,7 @@ def setup
7676
end
7777

7878
test "main form syncs its model and the models in nested sub-forms" do
79-
params = {
80-
name: "Euruco",
81-
city: "Athens",
82-
83-
speaker_attributes: {
84-
name: "Peter Markou",
85-
occupation: "Developer",
86-
87-
presentations_attributes: {
88-
"0" => { topic: "Ruby OOP", duration: "1h" },
89-
"1" => { topic: "Ruby Closures", duration: "1h" },
90-
}
91-
}
92-
}
93-
94-
@form.submit(params)
79+
@form.submit(default_params)
9580

9681
assert_equal "Euruco", @form.name
9782
assert_equal "Athens", @form.city
@@ -105,41 +90,12 @@ def setup
10590
end
10691

10792
test "main form validates itself" do
108-
params = {
109-
name: "Euruco",
110-
city: "Athens",
111-
112-
speaker_attributes: {
113-
name: "Petros Markou",
114-
occupation: "Developer",
115-
116-
presentations_attributes: {
117-
"0" => { topic: "Ruby OOP", duration: "1h" },
118-
"1" => { topic: "Ruby Closures", duration: "1h" },
119-
}
120-
}
121-
}
122-
123-
@form.submit(params)
124-
93+
@form.submit(merge_params(speaker_attributes: { name: 'Unique Name' }))
12594
assert @form.valid?
95+
end
12696

127-
params = {
128-
name: nil,
129-
city: nil,
130-
131-
speaker_attributes: {
132-
name: nil,
133-
occupation: nil,
134-
135-
presentations_attributes: {
136-
"0" => { topic: nil, duration: nil },
137-
"1" => { topic: nil, duration: nil },
138-
}
139-
}
140-
}
141-
142-
@form.submit(params)
97+
test "validation with empty params" do
98+
@form.submit({})
14399

144100
assert_not @form.valid?
145101
assert_includes @form.errors.messages[:name], "can't be blank"
@@ -153,23 +109,7 @@ def setup
153109
end
154110

155111
test "main form validates the models" do
156-
conference = conferences(:ruby)
157-
params = {
158-
name: conference.name,
159-
city: "Athens",
160-
161-
speaker_attributes: {
162-
name: conference.speaker.name,
163-
occupation: "Developer",
164-
165-
presentations_attributes: {
166-
"0" => { topic: "Ruby OOP", duration: "1h" },
167-
"1" => { topic: "Ruby Closures", duration: "1h" },
168-
}
169-
}
170-
}
171-
172-
@form.submit(params)
112+
@form.submit(speaker_attributes: { name: conferences(:ruby).speaker.name })
173113

174114
assert_not @form.valid?
175115
assert_includes @form.errors.messages[:name], "has already been taken"
@@ -198,22 +138,7 @@ def setup
198138
end
199139

200140
test "main form saves its model and the models in nested sub-forms" do
201-
params = {
202-
name: "Euruco",
203-
city: "Athens",
204-
205-
speaker_attributes: {
206-
name: "Petros Markou",
207-
occupation: "Developer",
208-
209-
presentations_attributes: {
210-
"0" => { topic: "Ruby OOP", duration: "1h" },
211-
"1" => { topic: "Ruby Closures", duration: "1h" },
212-
}
213-
}
214-
}
215-
216-
@form.submit(params)
141+
@form.submit(merge_params(speaker_attributes: { name: 'Petros Markou' }))
217142

218143
assert_difference(['Conference.count', 'Speaker.count']) do
219144
@form.save
@@ -237,23 +162,12 @@ def setup
237162
end
238163

239164
test "main form saves its model and dynamically added models in nested sub-forms" do
240-
params = {
241-
name: "Euruco",
242-
city: "Athens",
243-
244-
speaker_attributes: {
245-
name: "Petros Markou",
246-
occupation: "Developer",
247-
248-
presentations_attributes: {
249-
"0" => { topic: "Ruby OOP", duration: "1h" },
250-
"1" => { topic: "Ruby Closures", duration: "1h" },
251-
"1404292088779" => { topic: "Ruby Blocks", duration: "1h" }
252-
}
165+
@form.submit(merge_params(speaker_attributes: {
166+
name: "Petros Markou",
167+
presentations_attributes: {
168+
"1404292088779" => { topic: "Ruby Blocks", duration: "1h" }
253169
}
254-
}
255-
256-
@form.submit(params)
170+
}))
257171

258172
assert_difference(['Conference.count', 'Speaker.count']) do
259173
@form.save
@@ -281,30 +195,22 @@ def setup
281195
test "main form updates its model and the models in nested sub-forms" do
282196
conference = conferences(:ruby)
283197
form = ConferenceForm.new(conference)
284-
params = {
285-
name: "GoGaruco",
286-
city: "Golden State",
287-
198+
form.submit(merge_params(
288199
speaker_attributes: {
289-
name: "John Doe",
290-
occupation: "Developer",
291-
292200
presentations_attributes: {
293201
"0" => { topic: "Rails OOP", duration: "1h", id: presentations(:ruby_oop).id },
294-
"1" => { topic: "Rails Patterns", duration: "1h", id: presentations(:ruby_closures).id },
202+
"1" => { topic: "Rails Patterns", duration: "1h", id: presentations(:ruby_closures).id }
295203
}
296204
}
297-
}
298-
299-
form.submit(params)
205+
))
300206

301207
assert_difference(['Conference.count', 'Speaker.count', 'Presentation.count'], 0) do
302208
form.save
303209
end
304210

305-
assert_equal "GoGaruco", form.name
306-
assert_equal "Golden State", form.city
307-
assert_equal "John Doe", form.speaker.name
211+
assert_equal "Euruco", form.name
212+
assert_equal "Athens", form.city
213+
assert_equal "Peter Markou", form.speaker.name
308214
assert_equal "Developer", form.speaker.occupation
309215
assert_equal "Rails Patterns", form.speaker.presentations[0].topic
310216
assert_equal "1h", form.speaker.presentations[0].duration
@@ -318,31 +224,23 @@ def setup
318224
test "main form updates its model and saves dynamically added models in nested sub-forms" do
319225
conference = conferences(:ruby)
320226
form = ConferenceForm.new(conference)
321-
params = {
322-
name: "GoGaruco",
323-
city: "Golden State",
324-
227+
form.submit(merge_params(
325228
speaker_attributes: {
326-
name: "John Doe",
327-
occupation: "Developer",
328-
329229
presentations_attributes: {
330230
"0" => { topic: "Rails OOP", duration: "1h", id: presentations(:ruby_oop).id },
331231
"1" => { topic: "Rails Patterns", duration: "1h", id: presentations(:ruby_closures).id },
332232
"1404292088779" => { topic: "Rails Migrations", duration: "1h" }
333233
}
334234
}
335-
}
336-
337-
form.submit(params)
235+
))
338236

339237
assert_difference(['Conference.count', 'Speaker.count'], 0) do
340238
form.save
341239
end
342240

343-
assert_equal "GoGaruco", form.name
344-
assert_equal "Golden State", form.city
345-
assert_equal "John Doe", form.speaker.name
241+
assert_equal "Euruco", form.name
242+
assert_equal "Athens", form.city
243+
assert_equal "Peter Markou", form.speaker.name
346244
assert_equal "Developer", form.speaker.occupation
347245
assert_equal "Rails Patterns", form.speaker.presentations[0].topic
348246
assert_equal "1h", form.speaker.presentations[0].duration
@@ -358,32 +256,24 @@ def setup
358256
test "main form deletes models in nested sub-forms" do
359257
conference = conferences(:ruby)
360258
form = ConferenceForm.new(conference)
361-
params = {
362-
name: "GoGaruco",
363-
city: "Golden State",
364-
259+
form.submit(merge_params(
365260
speaker_attributes: {
366-
name: "John Doe",
367-
occupation: "Developer",
368-
369261
presentations_attributes: {
370262
"0" => { topic: "Rails OOP", duration: "1h", id: presentations(:ruby_oop).id, "_destroy" => "1" },
371-
"1" => { topic: "Rails Patterns", duration: "1h", id: presentations(:ruby_closures).id },
263+
"1" => { topic: "Rails Patterns", duration: "1h", id: presentations(:ruby_closures).id }
372264
}
373265
}
374-
}
375-
376-
form.submit(params)
266+
))
377267

378268
assert conference.speaker.presentations[1].marked_for_destruction?
379269

380270
assert_difference(['Conference.count', 'Speaker.count'], 0) do
381271
form.save
382272
end
383273

384-
assert_equal "GoGaruco", form.name
385-
assert_equal "Golden State", form.city
386-
assert_equal "John Doe", form.speaker.name
274+
assert_equal "Euruco", form.name
275+
assert_equal "Athens", form.city
276+
assert_equal "Peter Markou", form.speaker.name
387277
assert_equal "Developer", form.speaker.occupation
388278
assert_equal "Rails Patterns", form.speaker.presentations[0].topic
389279
assert_equal "1h", form.speaker.presentations[0].duration
@@ -398,31 +288,23 @@ def setup
398288
test "main form deletes and adds models in nested sub-forms" do
399289
conference = conferences(:ruby)
400290
form = ConferenceForm.new(conference)
401-
params = {
402-
name: "GoGaruco",
403-
city: "Golden State",
404-
291+
form.submit(merge_params(
405292
speaker_attributes: {
406-
name: "John Doe",
407-
occupation: "Developer",
408-
409293
presentations_attributes: {
410294
"0" => { topic: "Rails OOP", duration: "1h", id: presentations(:ruby_oop).id, "_destroy" => "1" },
411295
"1" => { topic: "Rails Patterns", duration: "1h", id: presentations(:ruby_closures).id },
412296
"1404292088779" => { topic: "Rails Testing", duration: "2h" }
413297
}
414298
}
415-
}
416-
417-
form.submit(params)
299+
))
418300

419301
assert_difference(['Conference.count', 'Speaker.count'], 0) do
420302
form.save
421303
end
422304

423-
assert_equal "GoGaruco", form.name
424-
assert_equal "Golden State", form.city
425-
assert_equal "John Doe", form.speaker.name
305+
assert_equal "Euruco", form.name
306+
assert_equal "Athens", form.city
307+
assert_equal "Peter Markou", form.speaker.name
426308
assert_equal "Developer", form.speaker.occupation
427309
assert_equal "Rails Patterns", form.speaker.presentations[0].topic
428310
assert_equal "1h", form.speaker.presentations[0].duration
@@ -445,27 +327,30 @@ def setup
445327
end
446328

447329
test "accepts file" do
448-
file = fixture_file_upload("demo.txt", "text/plain")
330+
@form.submit(photo: fixture_file_upload('demo.txt', 'text/plain')
449331

450-
params = {
451-
name: "GoGaruco",
452-
city: "Golden State",
453-
photo: file,
332+
assert @form.valid?
333+
assert_equal @form.photo, 'demo.txt'
334+
end
454335

455-
speaker_attributes: {
456-
name: "John Doe",
457-
occupation: "Developer",
336+
private
337+
def merge_params(params)
338+
default_params.deep_merge(params)
339+
end
458340

459-
presentations_attributes: {
460-
"0" => { topic: "Rails OOP", duration: "1h", id: presentations(:ruby_oop).id },
461-
"1" => { topic: "Rails Patterns", duration: "1h", id: presentations(:ruby_closures).id }
462-
}
463-
}
464-
}
341+
def default_params
342+
@default_params ||= {
343+
name: "Euruco", city: "Athens",
465344

466-
@form.submit(params)
345+
speaker_attributes: {
346+
name: "Peter Markou", occupation: "Developer",
467347

468-
assert @form.valid?
469-
assert_equal @form.photo, "demo.txt"
348+
presentations_attributes: {
349+
"0" => { topic: "Ruby OOP", duration: "1h" },
350+
"1" => { topic: "Ruby Closures", duration: "1h" },
351+
}
352+
}
353+
}
354+
end
470355
end
471356
end

0 commit comments

Comments
 (0)