Created
October 8, 2014 18:34
-
-
Save aaron/cafd4a693244f4b8079d to your computer and use it in GitHub Desktop.
Example functional test for a Rails 2.3 application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ActiveSupport::TestCase | |
def login_as(user) | |
@request.session[:user_id] = user ? users(user).id : nil | |
end | |
def current_user | |
@current_user ||= User.find(@request.session[:user_id]) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test_helper' | |
class UsersControllerTest < ActionController::TestCase | |
def setup | |
# most tests will run as monica | |
login_as :monica | |
end | |
test "should get index" do | |
get :index | |
assert_response :success | |
end | |
test "should redirect to login form for user who does not have access to index" do | |
# login as a user that does not have access to this action | |
login_as :tiffany | |
get :index | |
assert_redirected_to new_session_path | |
end | |
test "should get new" do | |
get :new | |
assert_response :success | |
end | |
test "should create user" do | |
assert_difference 'User.count' do | |
post :create, :user => { :active => true, :email => 'tester@example.com', :first_name => 'Jon', :last_name => 'Tester', :password => 'password', :password_confirmation => 'password' } | |
end | |
# use assigns to access the instance variable setup by the controller | |
assert_redirected_to user_path(assigns(:user)) | |
end | |
test "should fail to create user" do | |
assert_no_difference 'User.count' do | |
post :create, :user => { :first_name => 'Jon' } | |
end | |
# with a validation error the action renders a template and results in a success instead of a redirect | |
assert_response :success | |
end | |
test "should show user" do | |
get :show, :id => users(:tiffany).id | |
assert_response :success | |
end | |
test "should get edit" do | |
get :edit, :id => users(:tiffany).id | |
assert_response :success | |
end | |
test "should update user" do | |
put :update, :id => users(:tiffany).id, :user => { :last_name => 'Tester' } | |
assert_redirected_to user_path(assigns(:user)) | |
end | |
test "should fail to update user" do | |
put :update, :id => users(:tiffany).id, :user => { :last_name => '' } | |
# with a validation error the action renders a template and results in a success instead of a redirect | |
assert_response :success | |
end | |
test "should destroy user" do | |
assert_difference 'User.count', -1 do | |
delete :destroy, :id => users(:tiffany).id | |
end | |
assert_redirected_to users_path | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment