Skip to content

Instantly share code, notes, and snippets.

@dchelimsky
Created July 10, 2011 10:53
Show Gist options
  • Save dchelimsky/1074456 to your computer and use it in GitHub Desktop.
Save dchelimsky/1074456 to your computer and use it in GitHub Desktop.
class MacropostsController < ApplicationController
before_filter :authenticate
before_filter :authorized_user, :only => :destroy
def create
params[:macropost][:user_id] = current_user.id
params[:macropost][:project_id] = params[:project_id]
@macropost = Macropost.new(params[:macropost])
if @macropost.save
flash[:success] = "Macropost created!"
redirect_to project_path(@macropost.project)
else
render 'pages/home'
end
end
end
describe "POST 'create'" do
before(:each) do
@macropost = stub_model(Macropost)
Macropost.stub(:new).and_return(@macropost)
@current_user = Factory(:user)
test_sign_in(@current_user)
end
it "builds a new Macropost with the submitted macropost params" do
Macropost.should_receive(:new).with(hash_including(:this => :that))
post :create, :macropost => {:this => :that}
end
it "adds the project id to the macropost params" do
Macropost.should_receive(:new).with(hash_including(:project_id => 37))
post :create, :macropost => {}, :project_id => 37
end
it "adds the current user id to the macropost params" do
Macropost.should_receive(:new).with(hash_including(:user_id => @current_user.id))
post :create, :macropost => {}
end
describe "failure" do
it "renders the homepage" do
@macropost.stub(:save).and_return(false)
post :create, :macropost => {}
response.should render_template('pages/home')
end
end
describe "success" do
it "redirects to the project page" do
@macropost.stub(:save).and_return(true)
post :create, :macropost => {}
response.should redirect_to(project_path(@project))
end
it "creates a flash message" do
@macropost.stub(:save).and_return(true)
post :create, :macropost => {}
flash[:success].should =~ /macropost created/i
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment