Module: Terrys_spec_helpers

Defined in:
lib/terrys-spec-helpers.rb

Instance Method Summary collapse

Instance Method Details

#accessor_present(thing) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/terrys-spec-helpers.rb', line 9

def accessor_present(thing)
  describe "accessor #{thing}" do
    before do
      @o=@thing
      @o.should be_valid
      setup_variables(thing)
    end
    it 'should respond' do
      @o.respond_to?(@f).should be_true
    end
  end
end

#acts_as_a_listObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/terrys-spec-helpers.rb', line 22

def acts_as_a_list
  describe 'acts_as_list' do
    describe 'given a blank position on save' do
      before do
        @o=@thing
        @o.should be_valid
        setup_variables('position')
      end
      it 'should add itself to the list' do
        @o.send(@fp,nil)
        @o.save
        @o.send(@f).should_not be_nil
      end
    end
  end
end

#forbidden_string(thing, options = {}) ⇒ Object



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
# File 'lib/terrys-spec-helpers.rb', line 39

def forbidden_string(thing,options={})
  describe "FORBIDDEN attribute #{thing} (reverse captcha)" do
    before do
      if options[:pending]
        pending options[:pending]
      end
      @o=@thing
      @o.should be_valid
      setup_variables(thing)
    end
    it 'should respond' do
      @o.respond_to?(@f).should be_true
    end
    it "should accept a nil value" do
      @o.send(@fp,nil)
      @o.save
      @o.should be_valid
    end
    it "should accept a blank value" do
      @o.send(@fp,'')
      @o.save
      @o.should be_valid
    end
    it "should reject a non-blank value" do
      @o.send(@fp,'t')
      @o.save
      @o.should_not be_valid
    end
  end

end

#it_should_expose(variable) ⇒ Object



71
72
73
74
75
# File 'lib/terrys-spec-helpers.rb', line 71

def it_should_expose(variable)
  it "should expose the #{variable} variable" do
    controller.send(variable).should_not be_nil
  end
end

#it_should_return_200Object



77
78
79
80
81
# File 'lib/terrys-spec-helpers.rb', line 77

def it_should_return_200
  it "returns http success" do
    response.should be_success
  end
end

#it_should_set_flash_alertObject



83
84
85
86
87
# File 'lib/terrys-spec-helpers.rb', line 83

def it_should_set_flash_alert
  it "should set a flash alert" do
    flash[:alert].should_not be_nil
  end
end

#it_should_set_flash_noticeObject



89
90
91
92
93
# File 'lib/terrys-spec-helpers.rb', line 89

def it_should_set_flash_notice
  it "should set a flash notice" do
    flash[:notice].should_not be_nil
  end
end

#mandatory_belongs_to(thing, options = {}) ⇒ Object



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/terrys-spec-helpers.rb', line 332

def mandatory_belongs_to(thing,options={})
  describe "belongs_to association #{thing}" do
    before do
      if options[:pending]
        pending options[:pending]
      end
      f_id=thing.to_s.downcase+'_id'
      @o=@thing
      @o.should be_valid
      setup_variables(f_id)
      if options[:class_name]
        @klass=options[:class_name]
      else
        @klass=thing
      end
    end
    it 'should respond' do
      @o.respond_to?(@f).should be_true
    end
    it "should reject a nil value" do
      @o.send(@fp,nil)
      @o.save
      @o.should_not be_valid
    end
    it 'should reject an invalid value' do
      @klass.find(@o.send(@f)).should_not be_nil
      @o.should be_valid
      @o.send(@fp,999999)
      @klass.find_by_id(@o.send(@f)).should be_nil
      @o.should_not be_valid
    end
  end


end

#mandatory_boolean(thing) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/terrys-spec-helpers.rb', line 95

def mandatory_boolean(thing)
  describe "attribute #{thing}" do
    before do
      @o=@thing
      @o.should be_valid
      setup_variables(thing)
    end
    it 'should respond' do
      @o.respond_to?(@f).should be_true
    end
    it "should be invalid if #{thing} is not true" do
      @o.send(@fp,nil)
      @o.save.should_not be_true
    end
    it "should be valid if #{thing} is true" do
      @o.send(@fp,true)
      @o.save.should be_true
    end
  end
end

#mandatory_collection(thing, options = {}) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/terrys-spec-helpers.rb', line 116

def mandatory_collection(thing, options={})
  describe "association #{thing}" do
    before do
      if options[:pending]
        pending options[:pending]
      end
      @o=@thing
      @o.should be_valid
      setup_variables(thing)
    end
    it 'should respond' do
      @o.respond_to?(@f).should be_true
    end
  end
end

#mandatory_date(thing) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/terrys-spec-helpers.rb', line 132

def mandatory_date(thing)
  describe "attribute #{thing}" do
    before do
      @o=@thing
      @o.should be_valid
      setup_variables(thing)
    end
    it 'should respond' do
        @o.respond_to?(@f).should be_true
      end
    it "should reject a nil value" do
      @o.send(@fp,nil)
      @o.save
      @o.should_not be_valid
    end
    it "should reject a blank value" do
      @o.send(@fp,'')
      @o.save
      @o.should_not be_valid
    end
  end
end

#mandatory_datetime(thing, default = nil) ⇒ Object



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
# File 'lib/terrys-spec-helpers.rb', line 155

def mandatory_datetime(thing,default=nil)
  describe "attribute #{thing}" do
    before do
      @o=@thing
      @o.should be_valid
      setup_variables(thing)
    end
    it 'should respond' do
        @o.respond_to?(@f).should be_true
    end
    if default
      it "should set a nil value to the default if default exists" do
        @o.send(@fp,nil)
        @o.save
        @o.send(@f).should==default
      end
    else
      it "should reject a nil value if no default exists" do
        @o.send(@fp,nil)
        @o.save
        @o.should_not be_valid
      end
    end
    if default
      it "should set a blank value to the default if default exists" do
        @o.send(@fp,'')
        @o.save
        @o.send(@f).should==default
      end
    else
      it "should reject a blank value if no default exists" do
        @o.send(@fp,nil)
        @o.save
        @o.should_not be_valid
      end
    end
  end
end

#mandatory_float(thing) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/terrys-spec-helpers.rb', line 194

def mandatory_float(thing)
  describe "attribute #{thing}" do
    before do
      @o=@thing
      @o.should be_valid
      setup_variables(thing)
    end
    it 'should respond' do
      @o.respond_to?(@f).should be_true
    end
    it "should reject a nil value" do
      @o.send(@fp,nil)
      @o.save
      @o.should_not be_valid
    end
    it "should reject a blank value" do
      @o.send(@fp,'')
      @o.save
      @o.should_not be_valid
    end
  end
end

#mandatory_integer(thing) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/terrys-spec-helpers.rb', line 217

def mandatory_integer(thing)
  describe "attribute #{thing}" do
    before do
      @o=@thing
      @o.should be_valid
      setup_variables(thing)
    end
    it 'should respond' do
      @o.respond_to?(@f).should be_true
    end
    it "should reject a nil value" do
      @o.send(@fp,nil)
      @o.save
      @o.should_not be_valid
    end
    it "should reject a blank value" do
      @o.send(@fp,'')
      @o.save
      @o.should_not be_valid
    end
  end
end

#mandatory_integer_or_default(thing, default) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/terrys-spec-helpers.rb', line 240

def mandatory_integer_or_default(thing,default)
  describe "attribute #{thing}" do
    before do
      @o=@thing
      @o.should be_valid
      setup_variables(thing)
    end
    it 'should respond' do
      @o.respond_to?(@f).should be_true
    end
    it "should set a nil value to default" do
      @o.send(@fp,nil)
      @o.save
      @o.send(@f).should==default
    end
    it "should set a blank value to default" do
      @o.send(@fp,'')
      @o.save
      @o.send(@f).should==default
    end
  end
end

#mandatory_string(thing, options = {}) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/terrys-spec-helpers.rb', line 263

def mandatory_string(thing,options={})
  describe "attribute #{thing}" do
    before do
      if options[:pending]
        pending options[:pending]
      end
      @o=@thing
      @o.should be_valid
      setup_variables(thing)
    end
    it 'should respond' do
      @o.respond_to?(@f).should be_true
    end
    it "should reject a nil value" do
      @o.send(@fp,nil)
      @o.save
      @o.should_not be_valid
    end
    it "should reject a blank value" do
      @o.send(@fp,'')
      @o.save
      @o.should_not be_valid
    end
  end

end

#mandatory_string_or_default(thing, default) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/terrys-spec-helpers.rb', line 290

def mandatory_string_or_default(thing,default)
  describe "attribute #{thing}" do
    before do
      @o=@thing
      @o.should be_valid
      setup_variables(thing)
    end
    it 'should respond' do
      @o.respond_to?(@f).should be_true
    end
    it "should set a nil value to default" do
      @o.send(@fp,nil)
      @o.save
      @o.send(@f).should==default
    end
    it "should set a blank value to default" do
      @o.send(@fp,'')
      @o.save
      @o.send(@f).should==default
    end
  end
end

#mandatory_unique_string(thing) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/terrys-spec-helpers.rb', line 313

def mandatory_unique_string(thing)
  describe "unique attribute #{thing}" do
    mandatory_string(thing)
    before do
      @o=@thing
      @o.should be_valid
      setup_variables(thing)
    end
    it "should reject a duplicate value" do
      z=@o.class.new(@o.attributes)
      z.send(@f).should==@o.send(@f)
      z.should_not be_valid
      z.send(@fp,'zzz')
      z.should be_valid
    end
  end

end

#optional_accessor(thing, options = {}) ⇒ Object



368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/terrys-spec-helpers.rb', line 368

def optional_accessor(thing,options={})
  before do
    if options[:pending]
      pending options[:pending]
    end
    f_id=thing.to_s.downcase+'_id'
    @o=@thing
    @o.should be_valid
    setup_variables(f_id)
  end
  it 'should respond' do
    @o.respond_to?(@f).should be_true
  end
end

#optional_belongs_to(thing, options = {}) ⇒ Object



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/terrys-spec-helpers.rb', line 383

def optional_belongs_to(thing, options={})
  describe "belongs_to association #{thing}" do
    before do
      if options[:pending]
        pending options[:pending]
      end
      f_id=thing.to_s.downcase+'_id'
      @o=@thing
      @o.should be_valid
      setup_variables(f_id)
      if options[:class_name]
        @klass=options[:class_name]
      else
        @klass=thing
      end
    end
    it 'should respond' do
      @o.respond_to?(@f).should be_true
    end
    it 'should make an invalid value nil' do
      @o.send(@fp,999999)
      @klass.find_by_id(@o.send(@f)).should be_nil
      @o.valid?
      @o.send(@f).should be_nil
    end
  end
end

#optional_boolean(thing) ⇒ Object



411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/terrys-spec-helpers.rb', line 411

def optional_boolean(thing)
  describe "attribute #{thing}" do
    before do
      @o=@thing
      @o.should be_valid
      setup_variables(thing)
    end
    it 'should respond' do
      @o.respond_to?(@f).should be_true
    end
  end
end

#optional_date(thing) ⇒ Object



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/terrys-spec-helpers.rb', line 424

def optional_date(thing)
  describe "attribute #{thing}" do
    before do
      @o=@thing
      @o.should be_valid
      setup_variables(thing)
    end
    it 'should respond' do
      @o.respond_to?(@f).should be_true
    end
    it "should make a blank value nil" do
      @o.send(@fp,'')
      @o.save
      @o.send(@f).should be_nil
    end
  end
end

#optional_datetime(thing) ⇒ Object



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/terrys-spec-helpers.rb', line 442

def optional_datetime(thing)
  describe "attribute #{thing}" do
    before do
      @o=@thing
      @o.should be_valid
      setup_variables(thing)
    end
    it 'should respond' do
      @o.respond_to?(@f).should be_true
    end
    it "should make a blank value nil" do
      @o.send(@fp,'')
      @o.save
      @o.send(@f).should be_nil
    end
  end
end

#optional_float(thing) ⇒ Object



460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/terrys-spec-helpers.rb', line 460

def optional_float(thing)
  describe "attribute #{thing}" do
    before do
      @o=@thing
      @o.should be_valid
      setup_variables(thing)
    end
    it 'should respond' do
      @o.respond_to?(@f).should be_true
    end
    it "should make a blank value nil" do
      @o.send(@fp,'')
      @o.save
      @o.send(@f).should be_nil
    end
  end
end

#optional_integer(thing) ⇒ Object



478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/terrys-spec-helpers.rb', line 478

def optional_integer(thing)
  describe "attribute #{thing}" do
    before do
      @o=@thing
      @o.should be_valid
      setup_variables(thing)
    end
    it 'should respond' do
      @o.respond_to?(@f).should be_true
    end
    it "should make a blank value nil" do
      @o.send(@fp,'')
      @o.save
      @o.send(@f).should be_nil
    end
  end
end

#optional_string(thing) ⇒ Object



496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/terrys-spec-helpers.rb', line 496

def optional_string(thing)
  describe "attribute #{thing}" do
    before do
      @o=@thing
      @o.should be_valid
      setup_variables(thing)
    end
    it 'should respond' do
      @o.respond_to?(@f).should be_true
    end
    it "should make a blank value nil" do
      @o.send(@fp,'')
      @o.save
      @o.send(@f).should be_nil
    end
  end
end

#setup_variables(thing) ⇒ Object



3
4
5
6
7
# File 'lib/terrys-spec-helpers.rb', line 3

def setup_variables(thing)
  f=thing.to_s
  @f=f.to_sym
  @fp=(f+'=').to_sym
end

#there_can_be_only_one(thing, options = {}) ⇒ Object



514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/terrys-spec-helpers.rb', line 514

def there_can_be_only_one(thing,options={})
  describe "there can only be one item with attribute #{thing} set to true" do
    before do
      if options[:pending]
        pending options[:pending]
      end
      @o=@thing
      @o.should be_valid
      setup_variables(thing)
    end
    describe "where it is not the first item and #{thing} is true" do
      before do
        @z=@o.class.new(@o.attributes)
        if options[:attributes]
          @z.update_attributes(options[:attributes])
        end
        @o.send(@fp,false)
        @z.send(@fp,true)
        @z.save.should be_true
        @o.save.should be_true
        @z.send(@f).should be_true
        @o.send(@f).should be_false
      end
      it "should set to false any other items with #{thing} set to true" do
        @o.send(@fp,true)
        @o.save.should be_true
        @o.send(@f).should be_true
        @z.send(@f).should be_false
      end
    end
  end

end

#there_must_be_one(thing, options = {}) ⇒ Object



548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
# File 'lib/terrys-spec-helpers.rb', line 548

def there_must_be_one(thing,options={})
  describe "there must be at least one item with attribute #{thing} set to true" do
    before do
      if options[:pending]
        pending options[:pending]
      end
      @o=@thing
      @o.should be_valid
      setup_variables(thing)
    end
    describe "where it is the first item and #{thing} is false" do
      before do
       (@o.class.all-[@o]).each{|o| o.delete}
        @o.class.count.should==1
        @o.send(@fp,false)
        @o.send(@f).should be_false
      end
      it "it should set #{thing} to true" do
        @o.save
        @o.send(@f).should be_true
      end
    end
    describe "where it is the last item with #{thing} set to true" do
      before do
        @o.send(@fp,true)
        @o.save.should be_true
        @o.send(@f).should be_true
      end
      it "should not be able to be destroyed" do
        @o.destroy.should be_false
      end
    end
  end
end