Skip to content

Instantly share code, notes, and snippets.

@bnadlerjr
Created August 11, 2011 13:17
Show Gist options
  • Select an option

  • Save bnadlerjr/1139620 to your computer and use it in GitHub Desktop.

Select an option

Save bnadlerjr/1139620 to your computer and use it in GitHub Desktop.
Sample Rails Functional Test (Test::Unit / Shoulda)
require 'test_helper'
# Controller Testing Reminders
# * was the web request successful?
# * was the user redirected to the right page?
# * was the user successfully authenticated?
# * was the correct object stored in the response template?
# * was the appropriate message displayed to the user in the view?
class ProjectsControllerTest < ActionController::TestCase
test "on GET to index" do
get :index
assert_response :success
assert_assigns :projects
assert_template :index
end
test "on GET to new" do
get :new
assert_response :success
assert_assigns :project
assert_template :new
end
test "on POST to create with valid parameters" do
Project.any_instance.stubs(:save).returns(true)
post :create
assert_redirected_to projects_path
assert_flash /Successfully created/, :success
end
test "on POST to create with invalid parameters" do
Project.any_instance.stubs(:save).returns(false)
post :create
assert_redirected_to projects_path
assert_flash /not be created/, :error
end
test "on GET to show" do
Project.expects(:find).with(1).returns(Project.new)
get :show, :id => 1
assert_response :success
assert_assigns :project
end
test "on GET to edit" do
Project.expects(:find).with(1).returns(Project.new)
get :edit, :id => 1
assert_response :success
assert_assigns :project
assert_template :edit
end
test "on PUT to update with valid parameters" do
Project.expects(:find).returns(Project.new)
Project.any_instance.stubs(:update_attributes).returns(true)
put :update, :id => 1, :project => {}
assert_redirected_to projects_path
assert_flash /Successfully updated/, :success
end
test "on PUT to update with invalid parameters" do
Project.expects(:find).returns(Project.new)
Project.any_instance.stubs(:update_attributes).returns(false)
put :update, :id => 1, :project => {}
assert_redirected_to projects_path
assert_flash /not be updated/, :error
end
test "on DELETE to destroy" do
Project.expects(:find).with(1).returns(Project.new)
delete :destroy, :id => 1
assert_redirected_to projects_path
end
context "on GET to index" do
setup { get :index }
should("be successful") { assert_response :success }
should("assign to projects") { assert_assigns :projects }
should("render index template") { assert_template :index }
end
context "on GET to new" do
setup { get :new }
should("be successful") { assert_response :success }
should("assign to project") { assert_assigns :project }
should("render new template") { assert_template :new }
end
context "on POST to create" do
context "with valid parameters" do
setup do
Project.any_instance.stubs(:save).returns(true)
post :create
end
should("redirect to new") { assert_redirected_to projects_path }
should("set flash") { assert_flash /Successfully created/, :success }
end
context "with invalid parameters" do
setup do
Project.any_instance.stubs(:save).returns(false)
post :create
end
should("redirect to new") { assert_redirected_to projects_path }
should("set flash") { assert_flash /not be created/, :error }
end
end
context "on GET to show" do
setup do
Project.any_instance.stubs(:id).returns(1)
Project.expects(:find).with(1).returns(Project.new)
get :show, :id => 1
end
should("be successful") { assert_response :success }
should("assign to project") { assert_assigns :project }
end
context "on GET to edit" do
setup do
Project.expects(:find).with(1).returns(Project.new)
get :edit, :id => 1
end
should("be successful") { assert_response :success }
should("assign to project") { assert_assigns :project }
should("render edit template") { assert_template :edit }
end
context "on PUT to update" do
setup do
Project.expects(:find).with(1).returns(Project.new)
end
context "with valid parameters" do
setup do
Project.any_instance.expects(:update_attributes).returns(true)
put :update, :id => 1, :project => {}
end
should("redirect to projects") { assert_redirected_to projects_path }
should("set flash") { assert_flash /Successfully updated/, :success }
end
context "with invalid parameters" do
setup do
Project.any_instance.expects(:update_attributes).returns(false)
put :update, :id => 1, :project => {}
end
should("redirect to projects") { assert_redirected_to projects_path }
should("set flash") { assert_flash /not be updated/, :error }
end
end
context "on DELETE to destroy" do
setup do
Project.expects(:find).with(1).returns(Project.new)
delete :destroy, :id => 1
end
should("redirect to projects") { assert_redirected_to projects_path }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment