require File.dirname(__FILE__) + '/../test_helper' class CommentsControllerTest < ActionController::TestCase def setup Factory.create(:comment) end should_route :get, "posts/hello-world/comments", :post_id => 'hello-world', :controller => :comments, :action => :index should_route :get, "posts/hello-world/comments/new", :post_id => 'hello-world', :action => :new should_route :post, "posts/hello-world/comments", :post_id => 'hello-world', :action => :create should_route :get, "posts/hello-world/comments/1", :post_id => 'hello-world', :action => :show, :id => '1' should_route :get, "posts/hello-world/comments/1/edit", :post_id => 'hello-world', :action => :edit, :id => '1' should_route :put, "posts/hello-world/comments/1", :post_id => 'hello-world', :action => :update, :id => '1' should_route :delete, "posts/hello-world/comments/1", :post_id => 'hello-world', :action => :destroy, :id => '1' context "logged in" do setup do activate_authlogic UserSession.create(Factory.create(:user)) end context "on GET to :show for first record" do setup do get :show, :id => '1', :post_id => 'hello-world' end should_redirect_to('the comments post') { post_url('hello-world')} should_not_set_the_flash end context "on GET to :new" do setup do get :new, :post_id => 'hello-world' end should_assign_to :comment should_respond_with :success should_render_template :new should_not_set_the_flash end context "on GET to :index" do setup do get :index, :post_id => 'hello-world' end should_redirect_to('the comments post') { post_url('hello-world')} should_not_set_the_flash end context "on POST to :create" do setup do @old_count = Comment.count post :create, :comment => Factory.attributes_for(:comment), :post_id => 'hello-world' end should "create a new comment" do assert_equal @old_count + 1, Comment.count end should_assign_to :comment should_set_the_flash_to 'Create successful!' should_redirect_to('the comments post') { post_url('hello-world')} end context "on GET to :edit for first record" do setup do get :edit, :id => '1', :post_id => 'hello-world' end should_assign_to :comment should_respond_with :success should_render_template :edit should_not_set_the_flash end context "on PUT to :update" do setup do put :update, :id => '1', :post_id => 'hello-world', :comment => {} end should_assign_to :comment should_set_the_flash_to 'Save successful!' should_redirect_to('the comments post') { post_url('hello-world')} end context "on DELETE to :destroy" do setup do @old_count = Comment.count delete :destroy, :id => '1', :post_id => 'hello-world' end should "destroy comment" do assert_equal @old_count - 1, Comment.count end should_set_the_flash_to 'Record deleted!' should_redirect_to('the comments post') { post_url('hello-world')} end end context "not logged in" do context "on GET to :show for first record" do setup do get :show, :id => '1', :post_id => 'hello-world' end should_redirect_to('the comments post') { post_url('hello-world')} should_not_set_the_flash end context "on GET to :new" do setup do get :new, :post_id => 'hello-world' end should_assign_to :comment should_respond_with :success should_render_template :new should_not_set_the_flash end context "on GET to :index" do setup do get :index, :post_id => 'hello-world' end should_redirect_to('the comments post') { post_url('hello-world')} should_not_set_the_flash end context "on POST to :create" do setup do @old_count = Comment.count post :create, :comment => Factory.attributes_for(:comment, :author_name => 'David', :author_email => 'dave@example.com'), :post_id => 'hello-world' end should "create a new comment" do assert_equal @old_count + 1, Comment.count end should_assign_to :comment should_set_the_flash_to 'Create successful!' should_redirect_to('the comments post') { post_url('hello-world')} end context "on GET to :edit for first record" do setup do get :edit, :id => '1', :post_id => 'hello-world' end should_be_denied_access end context "on PUT to :update" do setup do put :update, :id => '1', :post_id => 'hello-world', :comment => {} end should_be_denied_access end context "on DELETE to :destroy" do setup do @old_count = Comment.count delete :destroy, :id => '1', :post_id => 'hello-world' end should "not destroy comment" do assert_equal @old_count, Comment.count end should_be_denied_access end end end