PageRenderTime 50ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/controllers/forums_controller_spec.rb

https://github.com/Epictetus/Rails-CMS-Wiki-Forum
Ruby | 161 lines | 132 code | 29 blank | 0 comment | 2 complexity | 5bf48fc21e15bb14d344dcf6d0eec5c7 MD5 | raw file
  1. require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
  2. describe ForumsController do
  3. def mock_user(stubs={})
  4. @mock_user ||= mock_model(User, stubs.merge({:is_admin? => true,
  5. :has_read_access_to? => true, :has_write_access_to? => true}))
  6. end
  7. def mock_forum(stubs={})
  8. @mock_forum ||= mock_model(Forum, stubs.merge({:name => 'cool forum', :message_posts => mock_message_posts}))
  9. end
  10. def mock_message_post(stubs={})
  11. @mock_message_post ||= mock_model(MessagePost, stubs.merge({}))
  12. end
  13. def mock_message_posts
  14. return @mock_message_posts if @mock_message_posts
  15. @mock_message_posts = mock('message_posts_proxy')
  16. @mock_message_posts.stub!(:paginate).and_return([mock_message_post].paginate)
  17. @mock_message_posts
  18. end
  19. before do
  20. ContentPage.should_receive(:get_side_menu).and_return(mock_model(ContentPage))
  21. ContentPage.should_receive(:get_top_menu).and_return(mock_model(ContentPage))
  22. controller.stub!(:current_user).and_return(mock_user)
  23. end
  24. describe "GET index" do
  25. it "assigns all forums as @forums" do
  26. Forum.stub(:all).and_return([mock_forum])
  27. get :index
  28. assigns[:forums].should == [mock_forum]
  29. end
  30. end
  31. describe "GET show" do
  32. before(:each) do
  33. Forum.stub!(:find).with("37").and_return(mock_forum)
  34. end
  35. it "assigns the requested forum as @forum" do
  36. get :show, :id => "37"
  37. assigns[:forum].should equal(mock_forum)
  38. end
  39. it "can render rss" do
  40. get :show, :id => "37", :format => :rss
  41. response.should render_template("show", :format => :rss)
  42. end
  43. end
  44. describe "GET new" do
  45. it "assigns a new forum as @forum" do
  46. Forum.stub!(:new).and_return(mock_forum)
  47. get :new
  48. assigns[:forum].should equal(mock_forum)
  49. end
  50. end
  51. describe "GET edit" do
  52. it "assigns the requested forum as @forum" do
  53. Forum.stub!(:find).with("37").and_return(mock_forum)
  54. get :edit, :id => "37"
  55. assigns[:forum].should equal(mock_forum)
  56. end
  57. end
  58. describe "POST create" do
  59. describe "with valid params" do
  60. it "assigns a newly created forum as @forum" do
  61. Forum.stub!(:new).with({'these' => 'params'}).and_return(mock_forum(:save => true))
  62. post :create, :forum => {:these => 'params'}
  63. assigns[:forum].should equal(mock_forum)
  64. end
  65. it "redirects to the created forum" do
  66. Forum.stub!(:new).and_return(mock_forum(:save => true))
  67. post :create, :forum => {}
  68. response.should redirect_to(forums_url)
  69. end
  70. end
  71. describe "with invalid params" do
  72. it "assigns a newly created but unsaved forum as @forum" do
  73. Forum.stub!(:new).with({'these' => 'params'}).and_return(mock_forum(:save => false))
  74. post :create, :forum => {:these => 'params'}
  75. assigns[:forum].should equal(mock_forum)
  76. end
  77. it "re-renders the 'new' template" do
  78. Forum.stub!(:new).and_return(mock_forum(:save => false))
  79. post :create, :forum => {}
  80. response.should render_template('new')
  81. end
  82. end
  83. end
  84. describe "PUT update" do
  85. describe "with valid params" do
  86. it "updates the requested forum" do
  87. Forum.should_receive(:find).with("37").and_return(mock_forum)
  88. mock_forum.should_receive(:update_attributes).with({'these' => 'params'})
  89. put :update, :id => "37", :forum => {:these => 'params'}
  90. end
  91. it "assigns the requested forum as @forum" do
  92. Forum.stub!(:find).and_return(mock_forum(:update_attributes => true))
  93. put :update, :id => "1"
  94. assigns[:forum].should equal(mock_forum)
  95. end
  96. it "redirects to the forum" do
  97. Forum.stub!(:find).and_return(mock_forum(:update_attributes => true))
  98. put :update, :id => "1"
  99. response.should redirect_to(forum_url(mock_forum))
  100. end
  101. end
  102. describe "with invalid params" do
  103. it "updates the requested forum" do
  104. Forum.should_receive(:find).with("37").and_return(mock_forum)
  105. mock_forum.should_receive(:update_attributes).with({'these' => 'params'})
  106. put :update, :id => "37", :forum => {:these => 'params'}
  107. end
  108. it "assigns the forum as @forum" do
  109. Forum.stub!(:find).and_return(mock_forum(:update_attributes => false))
  110. put :update, :id => "1"
  111. assigns[:forum].should equal(mock_forum)
  112. end
  113. it "re-renders the 'edit' template" do
  114. Forum.stub!(:find).and_return(mock_forum(:update_attributes => false))
  115. put :update, :id => "1"
  116. response.should render_template('edit')
  117. end
  118. end
  119. end
  120. describe "DELETE destroy" do
  121. it "destroys the requested forum" do
  122. Forum.should_receive(:find).with("37").and_return(mock_forum)
  123. mock_forum.should_receive(:destroy)
  124. delete :destroy, :id => "37"
  125. end
  126. it "redirects to the forums list" do
  127. Forum.stub!(:find).and_return(mock_forum(:destroy => true))
  128. delete :destroy, :id => "1"
  129. response.should redirect_to(forums_url)
  130. end
  131. end
  132. end