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