Skip to content

Instantly share code, notes, and snippets.

@GBH
Last active October 9, 2015 06:17
Show Gist options
  • Save GBH/3452023 to your computer and use it in GitHub Desktop.
Save GBH/3452023 to your computer and use it in GitHub Desktop.
Textmate snippet for `controllertest`. Scope `source.ruby`
require_relative '../test_helper'
class ${1:Model}sControllerTest < ActionController::TestCase
def setup
@${1/./\l$0/} = ${1/./\l$0/}s(:default)
end
def test_get_index
get :index
assert_response :success
assert assigns(:${1/./\l$0/}s)
assert_template :index
end
def test_get_show
get :show, id: @${1/./\l$0/}
assert_response :success
assert assigns(:${1/./\l$0/})
assert_template :show
end
def test_get_show_failure
get :show, id: 'invalid'
assert_response :redirect
assert_redirected_to action: :index
assert_equal '${1:Model} not found', flash[:error]
end
def test_get_new
get :new
assert_response :success
assert assigns(:${1/./\l$0/})
assert_template :new
assert_select 'form[action=/${1/./\l$0/}s]'
end
def test_get_edit
get :edit, id: @${1/./\l$0/}
assert_response :success
assert assigns(:${1/./\l$0/})
assert_template :edit
assert_select "form[action=/${1/./\l$0/}s/#{@${1/./\l$0/}.id}]"
end
def test_creation
assert_difference '${1:Model}.count' do
post :create, ${1/./\l$0/}: {
attribute: ''
}
${1/./\l$0/} = ${1:Model}.last
assert_response :redirect
assert_redirected_to action: :show, id: ${1/./\l$0/}
assert_equal '${1:Model} created', flash[:success]
end
end
def test_creation_failure
assert_no_difference '${1:Model}.count' do
post :create, ${1/./\l$0/}: { }
assert_response :success
assert_template :new
assert_equal 'Failed to create ${1:Model}', flash[:error]
end
end
def test_update
put :update, id: @${1/./\l$0/}, ${1/./\l$0/}: {
attribute: 'Updated'
}
assert_response :redirect
assert_redirected_to action: :show, id: @${1/./\l$0/}
assert_equal '${1:Model} updated', flash[:success]
@${1/./\l$0/}.reload
assert_equal 'Updated', @${1/./\l$0/}.attribute
end
def test_update_failure
${1/./\l$0/} = ${1/./\l$0/}s(:default)
put :update, id: ${1/./\l$0/}, ${1/./\l$0/}: {
attribute: ''
}
assert_response :success
assert_template :edit
assert_equal 'Failed to update ${1:Model}', flash[:error]
${1/./\l$0/}.reload
refute_equal '', ${1/./\l$0/}.attribute
end
def test_destroy
assert_difference '${1:Model}.count', -1 do
delete :destroy, id: @${1/./\l$0/}
assert_response :redirect
assert_redirected_to action: :index
assert_equal '${1:Model} deleted', flash[:success]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment