diff options
Diffstat (limited to 'railties/lib/rails')
4 files changed, 37 insertions, 25 deletions
diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb index 243694f38e..658d883883 100644 --- a/railties/lib/rails/generators/named_base.rb +++ b/railties/lib/rails/generators/named_base.rb @@ -129,6 +129,18 @@ module Rails uncountable? ? "#{plural_table_name}_index" : plural_table_name end + def show_helper + "#{singular_table_name}_url(@#{singular_table_name})" + end + + def edit_helper + "edit_#{show_helper}" + end + + def new_helper + "new_#{singular_table_name}_url" + end + def singular_table_name @singular_table_name ||= (pluralize_table_names? ? table_name.singularize : table_name) end diff --git a/railties/lib/rails/generators/test_unit/controller/templates/functional_test.rb b/railties/lib/rails/generators/test_unit/controller/templates/functional_test.rb index 5a8a3ca5e0..4f2ceb8589 100644 --- a/railties/lib/rails/generators/test_unit/controller/templates/functional_test.rb +++ b/railties/lib/rails/generators/test_unit/controller/templates/functional_test.rb @@ -1,11 +1,9 @@ require 'test_helper' <% module_namespacing do -%> -class <%= class_name %>ControllerTest < ActionController::TestCase +class <%= class_name %>ControllerTest < ActionDispatch::IntegrationTest <% if mountable_engine? -%> - setup do - @routes = Engine.routes - end + include Engine.routes.url_helpers <% end -%> <% if actions.empty? -%> @@ -15,7 +13,7 @@ class <%= class_name %>ControllerTest < ActionController::TestCase <% else -%> <% actions.each do |action| -%> test "should get <%= action %>" do - get :<%= action %> + get <%= file_name %>_<%= action %>_url assert_response :success end diff --git a/railties/lib/rails/generators/test_unit/scaffold/templates/api_functional_test.rb b/railties/lib/rails/generators/test_unit/scaffold/templates/api_functional_test.rb index f302cd6c3d..de5814eae9 100644 --- a/railties/lib/rails/generators/test_unit/scaffold/templates/api_functional_test.rb +++ b/railties/lib/rails/generators/test_unit/scaffold/templates/api_functional_test.rb @@ -1,40 +1,41 @@ require 'test_helper' <% module_namespacing do -%> -class <%= controller_class_name %>ControllerTest < ActionController::TestCase +class <%= controller_class_name %>ControllerTest < ActionDispatch::IntegrationTest + <% if mountable_engine? -%> + include Engine.routes.url_helpers + + <% end -%> setup do @<%= singular_table_name %> = <%= fixture_name %>(:one) -<% if mountable_engine? -%> - @routes = Engine.routes -<% end -%> end test "should get index" do - get :index + get <%= index_helper %>_url assert_response :success end test "should create <%= singular_table_name %>" do assert_difference('<%= class_name %>.count') do - post :create, params: { <%= "#{singular_table_name}: { #{attributes_hash} }" %> } + post <%= index_helper %>_url, params: { <%= "#{singular_table_name}: { #{attributes_hash} }" %> } end assert_response 201 end test "should show <%= singular_table_name %>" do - get :show, params: { id: <%= "@#{singular_table_name}" %> } + get <%= show_helper %> assert_response :success end test "should update <%= singular_table_name %>" do - patch :update, params: { id: <%= "@#{singular_table_name}" %>, <%= "#{singular_table_name}: { #{attributes_hash} }" %> } + patch <%= show_helper %>, params: { <%= "#{singular_table_name}: { #{attributes_hash} }" %> } assert_response 200 end test "should destroy <%= singular_table_name %>" do assert_difference('<%= class_name %>.count', -1) do - delete :destroy, params: { id: <%= "@#{singular_table_name}" %> } + delete <%= show_helper %> end assert_response 204 diff --git a/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb b/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb index 50b98b2631..1989d84c79 100644 --- a/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb +++ b/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb @@ -1,50 +1,51 @@ require 'test_helper' <% module_namespacing do -%> -class <%= controller_class_name %>ControllerTest < ActionController::TestCase +class <%= controller_class_name %>ControllerTest < ActionDispatch::IntegrationTest + <%- if mountable_engine? -%> + include Engine.routes.url_helpers + <% end -%> + setup do @<%= singular_table_name %> = <%= fixture_name %>(:one) -<% if mountable_engine? -%> - @routes = Engine.routes -<% end -%> end test "should get index" do - get :index + get <%= index_helper %>_url assert_response :success end test "should get new" do - get :new + get <%= new_helper %> assert_response :success end test "should create <%= singular_table_name %>" do assert_difference('<%= class_name %>.count') do - post :create, params: { <%= "#{singular_table_name}: { #{attributes_hash} }" %> } + post <%= index_helper %>_url, params: { <%= "#{singular_table_name}: { #{attributes_hash} }" %> } end assert_redirected_to <%= singular_table_name %>_path(<%= class_name %>.last) end test "should show <%= singular_table_name %>" do - get :show, params: { id: <%= "@#{singular_table_name}" %> } + get <%= show_helper %> assert_response :success end test "should get edit" do - get :edit, params: { id: <%= "@#{singular_table_name}" %> } + get <%= edit_helper %> assert_response :success end test "should update <%= singular_table_name %>" do - patch :update, params: { id: <%= "@#{singular_table_name}" %>, <%= "#{singular_table_name}: { #{attributes_hash} }" %> } + patch <%= show_helper %>, params: { <%= "#{singular_table_name}: { #{attributes_hash} }" %> } assert_redirected_to <%= singular_table_name %>_path(<%= "@#{singular_table_name}" %>) end test "should destroy <%= singular_table_name %>" do assert_difference('<%= class_name %>.count', -1) do - delete :destroy, params: { id: <%= "@#{singular_table_name}" %> } + delete <%= show_helper %> end assert_redirected_to <%= index_helper %>_path |