/
forums_controller_spec.rb
728 lines (575 loc) · 18.2 KB
/
forums_controller_spec.rb
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
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
547
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper'))
require File.expand_path(File.join(File.dirname(__FILE__), '../app'))
describe "Routing shortcuts for Forums should map" do
controller_name :forums
before(:each) do
@forum = mock('Forum')
@forum.stub!(:to_param).and_return('2')
Forum.stub!(:find).and_return(@forum)
get :show, :id => "2"
end
it "resources_path to /forums" do
controller.resources_path.should == '/forums'
end
it "resources_path(:foo => 'bar') to /forums?foo=bar" do
controller.resources_path(:foo => 'bar').should == '/forums?foo=bar'
end
it "resource_path to /forums/2" do
controller.resource_path.should == '/forums/2'
end
it "resource_path(:foo => 'bar') to /forums/2?foo=bar" do
controller.resource_path(:foo => 'bar').should == '/forums/2?foo=bar'
end
it "resource_path(9) to /forums/9" do
controller.resource_path(9).should == '/forums/9'
end
it "resource_path(9, :foo => 'bar') to /forums/2?foo=bar" do
controller.resource_path(9, :foo => 'bar').should == '/forums/9?foo=bar'
end
it "edit_resource_path to /forums/2/edit" do
controller.edit_resource_path.should == '/forums/2/edit'
end
it "edit_resource_path(9) to /forums/9/edit" do
controller.edit_resource_path(9).should == '/forums/9/edit'
end
it "new_resource_path to /forums/new" do
controller.new_resource_path.should == '/forums/new'
end
it "resources_url to http://test.host/forums" do
controller.resources_url.should == 'http://test.host/forums'
end
it "resource_url to http://test.host/forums/2" do
controller.resource_url.should == 'http://test.host/forums/2'
end
it "resource_url(9) to http://test.host/forums/9" do
controller.resource_url(9).should == 'http://test.host/forums/9'
end
it "edit_resource_url to http://test.host/forums/2/edit" do
controller.edit_resource_url.should == 'http://test.host/forums/2/edit'
end
it "edit_resource_url(9) to http://test.host/forums/9/edit" do
controller.edit_resource_url(9).should == 'http://test.host/forums/9/edit'
end
it "new_resource_url to http://test.host/forums/new" do
controller.new_resource_url.should == 'http://test.host/forums/new'
end
it "resource_interests_path to /forums/2/interests" do
controller.resource_interests_path.should == "/forums/2/interests"
end
it "resource_interests_path(:foo => 'bar') to /forums/2/interests?foo=bar" do
controller.resource_interests_path(:foo => 'bar').should == '/forums/2/interests?foo=bar'
end
it "resource_interests_path(9) to /forums/9/interests" do
controller.resource_interests_path(9).should == "/forums/9/interests"
end
it "resource_interests_path(9, :foo => 'bar') to /forums/9/interests?foo=bar" do
controller.resource_interests_path(9, :foo => 'bar').should == "/forums/9/interests?foo=bar"
end
it "resource_interest_path(5) to /forums/2/interests/5" do
controller.resource_interest_path(5).should == "/forums/2/interests/5"
end
it "resource_interest_path(9,5) to /forums/9/interests/5" do
controller.resource_interest_path(9,5).should == "/forums/9/interests/5"
end
it "resource_interest_path(9,5, :foo => 'bar') to /forums/9/interests/5?foo=bar" do
controller.resource_interest_path(9, 5, :foo => 'bar').should == "/forums/9/interests/5?foo=bar"
end
it 'new_resource_interest_path(9) to /forums/9/interests/new' do
controller.new_resource_interest_path(9).should == "/forums/9/interests/new"
end
it 'edit_resource_interest_path(5) to /forums/2/interests/5/edit' do
controller.edit_resource_interest_path(5).should == "/forums/2/interests/5/edit"
end
it 'edit_resource_interest_path(9,5) to /forums/9/interests/5/edit' do
controller.edit_resource_interest_path(9,5).should == "/forums/9/interests/5/edit"
end
it "respond_to?(:edit_resource_interest_path) should == true" do
controller.should respond_to(:edit_resource_interest_path)
end
it "resource_users_path should raise informative NoMethodError" do
lambda{ controller.resource_users_path }.should raise_error(Ardes::ResourcesController::CantMapRoute, <<-end_str
Tried to map :resource_users_path to :forum_users_path,
which doesn't exist. You may not have defined the route in config/routes.rb.
Or, if you have unconventianal route names or name prefixes, you may need
to explicictly set the :route option in resources_controller_for, and set
the :name_prefix option on your enclosing resources.
Currently:
:route is 'forum'
generated name_prefix is ''
end_str
)
end
it "enclosing_resource_path should raise informative NoMethodError" do
lambda{ controller.enclosing_resource_path }.should raise_error(NoMethodError, "Tried to map :enclosing_resource_path but there is no enclosing_resource for this controller")
end
it "any_old_missing_method should raise NoMethodError" do
lambda{ controller.any_old_missing_method }.should raise_error(NoMethodError)
end
it "respond_to?(:resource_users_path) should == false" do
controller.should_not respond_to(:resource_users_path)
end
end
describe ForumsController, " (checking that non actions are hidden)" do
it "should only have CRUD actions as action_methods" do
(@controller.class.send(:action_methods) & Set.new(['resource', 'resources'])).should be_empty
end
end
describe "resource_service in ForumsController" do
controller_name :forums
before(:each) do
@forum = Forum.create
get :index
@resource_service = controller.send :resource_service
end
it "should build new forum with new" do
resource = @resource_service.new
resource.should be_kind_of(Forum)
end
it "should find @forum with find(@forum.id)" do
resource = @resource_service.find(@forum.id)
resource.should == @forum
end
it "should find all forums with find(:all)" do
resources = @resource_service.find(:all)
resources.should == Forum.find(:all)
end
end
describe ForumsController, " requesting / (testing resource_path)" do
it "should generate params { :controller => 'forums', :action => 'index', :resource_path => '/forums' } from GET /" do
params_from(:get, "/").should == { :controller => 'forums', :action => 'index', :resource_path => '/forums' }
end
before(:each) do
@mock_forums = mock('forums')
Forum.stub!(:find).and_return(@mock_forums)
end
def do_get
get :index, :resource_path => '/forums'
end
it "should be successful" do
do_get
response.should be_success
end
it "should render index.rhtml" do
do_get
response.should render_template(:index)
end
it "should find all forums" do
Forum.should_receive(:find).with(:all).and_return(@mock_forums)
do_get
end
it "should assign the found forums for the view" do
do_get
assigns[:forums].should == @mock_forums
end
end
describe ForumsController, " requesting /create_forum (testing resource_method)" do
it "should generate params { :controller => 'forums', :action => 'create', :resource_path => '/forums', :resource_method => :post } from GET /create_forum" do
params_from(:get, "/create_forum").should == { :controller => 'forums', :action => 'create', :resource_path => '/forums', :resource_method => :post }
end
before(:each) do
@mock_forum = mock('Forum')
@mock_forum.stub!(:save).and_return(true)
@mock_forum.stub!(:to_param).and_return("1")
Forum.stub!(:new).and_return(@mock_forum)
end
def do_post
post :create, :forum => {:name => 'Forum'}, :resource_path => '/forums', :resource_method => :post
end
it "should create a new forum" do
Forum.should_receive(:new).with({'name' => 'Forum'}).and_return(@mock_forum)
do_post
end
it "should set the flash notice" do
do_post
flash[:notice].should == "Forum was successfully created."
end
it "should redirect to the new forum" do
do_post
response.should be_redirect
response.redirect_url.should == "http://test.host/forums/1"
end
end
describe "Requesting /forums using GET" do
controller_name :forums
before(:each) do
@mock_forums = mock('forums')
Forum.stub!(:find).and_return(@mock_forums)
end
def do_get
get :index
end
it "should be successful" do
do_get
response.should be_success
end
it "should render index.rhtml" do
do_get
response.should render_template(:index)
end
it "should find all forums" do
Forum.should_receive(:find).with(:all).and_return(@mock_forums)
do_get
end
it "should assign the found forums for the view" do
do_get
assigns[:forums].should == @mock_forums
end
end
describe "Requesting /forums.xml using GET" do
controller_name :forums
before(:each) do
@mock_forums = mock('forums')
@mock_forums.stub!(:to_xml).and_return("XML")
Forum.stub!(:find).and_return(@mock_forums)
end
def do_get
@request.env["HTTP_ACCEPT"] = "application/xml"
get :index
end
it "should be successful" do
do_get
response.should be_success
end
it "should find all forums" do
Forum.should_receive(:find).with(:all).and_return(@mock_forums)
do_get
end
it "should render the found forums as xml" do
@mock_forums.should_receive(:to_xml).and_return("XML")
do_get
response.body.should eql("XML")
end
end
describe "Requesting /forums using XHR GET" do
controller_name :forums
before(:each) do
@mock_forums = mock('forums')
Forum.stub!(:find).and_return(@mock_forums)
end
def do_get
@request.env["HTTP_ACCEPT"] = "text/javascript"
xhr :get, :index
end
it "should be successful" do
do_get
response.should be_success
end
it "should find all forums" do
Forum.should_receive(:find).with(:all).and_return(@mock_forums)
do_get
end
it "should render index.rjs" do
do_get
response.should render_template('index')
end
end
describe "Requesting /forums/1 using GET" do
controller_name :forums
before(:each) do
@mock_forum = mock('Forum')
Forum.stub!(:find).and_return(@mock_forum)
end
def do_get
get :show, :id => "1"
end
it "should be successful" do
do_get
response.should be_success
end
it "should render show.rhtml" do
do_get
response.should render_template(:show)
end
it "should find the forum requested" do
Forum.should_receive(:find).with("1").and_return(@mock_forum)
do_get
end
it "should assign the found forum for the view" do
do_get
assigns[:forum].should == @mock_forum
end
end
describe "Requesting /forums/1.xml using GET" do
controller_name :forums
before(:each) do
@mock_forum = mock('Forum')
@mock_forum.stub!(:to_xml).and_return("XML")
Forum.stub!(:find).and_return(@mock_forum)
end
def do_get
@request.env["HTTP_ACCEPT"] = "application/xml"
get :show, :id => "1"
end
it "should be successful" do
do_get
response.should be_success
end
it "should find the forum requested" do
Forum.should_receive(:find).with("1").and_return(@mock_forum)
do_get
end
it "should render the found forum as xml" do
@mock_forum.should_receive(:to_xml).and_return("XML")
do_get
response.body.should eql("XML")
end
end
describe "Requesting /forums/1 using XHR GET" do
controller_name :forums
before(:each) do
@mock_forum = mock('Forum')
Forum.stub!(:find).and_return(@mock_forum)
end
def do_get
xhr :get, :show, :id => "1"
end
it "should be successful" do
do_get
response.should be_success
end
it "should render show.rjs" do
do_get
response.should render_template('show')
end
it "should find the forum requested" do
Forum.should_receive(:find).with("1").and_return(@mock_forum)
do_get
end
it "should assign the found forum for the view" do
do_get
assigns[:forum].should == @mock_forum
end
end
describe "Requesting /forums/new using GET" do
controller_name :forums
before(:each) do
@mock_forum = mock('Forum')
Forum.stub!(:new).and_return(@mock_forum)
end
def do_get
get :new
end
it "should be successful" do
do_get
response.should be_success
end
it "should render new.rhtml" do
do_get
response.should render_template(:new)
end
it "should create an new forum" do
Forum.should_receive(:new).and_return(@mock_forum)
do_get
end
it "should not save the new forum" do
@mock_forum.should_not_receive(:save)
do_get
end
it "should assign the new forum for the view" do
do_get
assigns[:forum].should == @mock_forum
end
end
describe "Requesting /forums/1/edit using GET" do
controller_name :forums
before(:each) do
@mock_forum = mock('Forum')
Forum.stub!(:find).and_return(@mock_forum)
end
def do_get
get :edit, :id => "1"
end
it "should be successful" do
do_get
response.should be_success
end
it "should render edit.rhtml" do
do_get
response.should render_template(:edit)
end
it "should find the forum requested" do
Forum.should_receive(:find).and_return(@mock_forum)
do_get
end
it "should assign the found Forum for the view" do
do_get
assigns(:forum).should equal(@mock_forum)
end
end
describe "Requesting /forums using POST" do
controller_name :forums
before(:each) do
@mock_forum = mock('Forum')
@mock_forum.stub!(:save).and_return(true)
@mock_forum.stub!(:to_param).and_return("1")
Forum.stub!(:new).and_return(@mock_forum)
end
def do_post
post :create, :forum => {:name => 'Forum'}
end
it "should create a new forum" do
Forum.should_receive(:new).with({'name' => 'Forum'}).and_return(@mock_forum)
do_post
end
it "should set the flash notice" do
do_post
flash[:notice].should == "Forum was successfully created."
end
it "should redirect to the new forum" do
do_post
response.should be_redirect
response.redirect_url.should == "http://test.host/forums/1"
end
end
describe "Requesting /forums using XHR POST" do
controller_name :forums
before(:each) do
@mock_forum = mock('Forum')
@mock_forum.stub!(:save).and_return(true)
@mock_forum.stub!(:to_param).and_return("1")
Forum.stub!(:new).and_return(@mock_forum)
end
def do_post
xhr :post, :create, :forum => {:name => 'Forum'}
end
it "should create a new forum" do
Forum.should_receive(:new).with({'name' => 'Forum'}).and_return(@mock_forum)
do_post
end
it "should not set the flash notice" do
do_post
flash[:notice].should == nil
end
it "should render create.rjs" do
do_post
response.should render_template('create')
end
it "should render new.rjs if unsuccesful" do
@mock_forum.stub!(:save).and_return(false)
do_post
response.should render_template('new')
end
end
describe "Requesting /forums/1 using PUT" do
controller_name :forums
before(:each) do
@mock_forum = mock('Forum', :null_object => true)
@mock_forum.stub!(:to_param).and_return("1")
Forum.stub!(:find).and_return(@mock_forum)
end
def do_update
put :update, :id => "1"
end
it "should find the forum requested" do
Forum.should_receive(:find).with("1").and_return(@mock_forum)
do_update
end
it "should set the flash notice" do
do_update
flash[:notice].should == "Forum was successfully updated."
end
it "should update the found forum" do
@mock_forum.should_receive(:update_attributes)
do_update
assigns(:forum).should == @mock_forum
end
it "should assign the found forum for the view" do
do_update
assigns(:forum).should == @mock_forum
end
it "should redirect to the forum" do
do_update
response.should be_redirect
response.redirect_url.should == "http://test.host/forums/1"
end
end
describe "Requesting /forums/1 using XHR PUT" do
controller_name :forums
before(:each) do
@mock_forum = mock('Forum', :null_object => true)
@mock_forum.stub!(:to_param).and_return("1")
Forum.stub!(:find).and_return(@mock_forum)
end
def do_update
xhr :put, :update, :id => "1"
end
it "should find the forum requested" do
Forum.should_receive(:find).with("1").and_return(@mock_forum)
do_update
end
it "should update the found forum" do
@mock_forum.should_receive(:update_attributes)
do_update
assigns(:forum).should == @mock_forum
end
it "should not set the flash notice" do
do_update
flash[:notice].should == nil
end
it "should assign the found forum for the view" do
do_update
assigns(:forum).should == @mock_forum
end
it "should render update.rjs" do
do_update
response.should render_template('update')
end
it "should render edit.rjs, on unsuccessful save" do
@mock_forum.stub!(:update_attributes).and_return(false)
do_update
response.should render_template('edit')
end
end
describe "Requesting /forums/1 using DELETE" do
controller_name :forums
before(:each) do
@mock_forum = mock('Forum', :null_object => true)
Forum.stub!(:find).and_return(@mock_forum)
end
def do_delete
delete :destroy, :id => "1"
end
it "should find the forum requested" do
Forum.should_receive(:find).with("1").and_return(@mock_forum)
do_delete
end
it "should call destroy on the found forum" do
@mock_forum.should_receive(:destroy)
do_delete
end
it "should set the flash notice" do
do_delete
flash[:notice].should == 'Forum was successfully destroyed.'
end
it "should redirect to the forums list" do
do_delete
response.should be_redirect
response.redirect_url.should == "http://test.host/forums"
end
end
describe "Requesting /forums/1 using XHR DELETE" do
controller_name :forums
before(:each) do
@mock_forum = mock('Forum', :null_object => true)
Forum.stub!(:find).and_return(@mock_forum)
end
def do_delete
xhr :delete, :destroy, :id => "1"
end
it "should find the forum requested" do
Forum.should_receive(:find).with("1").and_return(@mock_forum)
do_delete
end
it "should not set the flash notice" do
do_delete
flash[:notice].should == nil
end
it "should call destroy on the found forum" do
@mock_forum.should_receive(:destroy)
do_delete
end
it "should render destroy.rjs" do
do_delete
response.should render_template('destroy')
end
end