Skip to content

Instantly share code, notes, and snippets.

@gotascii
Forked from dce/posts_controller.rb
Created February 17, 2011 16:35
Show Gist options
  • Save gotascii/832055 to your computer and use it in GitHub Desktop.
Save gotascii/832055 to your computer and use it in GitHub Desktop.
def PostsController < ActionController::Base
def create
@post = Post.new(params[:post])
if @post.save
redirect_to posts_path
else
render :action => "new"
end
end
end
class PostsControllerTest < ActionController::TestCase
context "A PostsController" do
context "on POST to :create" do
setup do
@post = Factory.build(:post)
Post.stubs(:new).returns(@post)
end
should "create a new post with params" do
params = {'some' => 'params'}
Post.expects(:new).with(params).returns(post)
post :create, :post => params
end
should "assign the new post" do
post :create
assigns(:post).should == @post
end
should "try to save the new post" do
@post.expects(:save)
post :create
end
context "with valid input" do
setup do
@post.stubs(:save).returns(true)
post :create
end
should_redirect_to("post index") { posts_path }
end
context "with invalid input" do
setup do
@post.stubs(:save).returns(false)
post :create
end
should_respond_with :success
should_render_template :new
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment