From d502cd1a5024743b3891592c73538d4e446bbd98 Mon Sep 17 00:00:00 2001 From: Dinah Shi Date: Sat, 10 Jun 2017 21:39:51 -0400 Subject: Add system tests to generate scaffold --- .../rails/scaffold/scaffold_generator.rb | 3 -- .../test_unit/scaffold/scaffold_generator.rb | 19 +++++++-- .../scaffold/templates/api_functional_test.rb | 4 +- .../scaffold/templates/functional_test.rb | 4 +- .../test_unit/scaffold/templates/system_test.rb | 49 ++++++++++++++++++++++ 5 files changed, 68 insertions(+), 11 deletions(-) create mode 100644 railties/lib/rails/generators/test_unit/scaffold/templates/system_test.rb (limited to 'railties/lib/rails/generators') diff --git a/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb index 12d6bc85b2..9522778c09 100644 --- a/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb +++ b/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb @@ -16,13 +16,10 @@ module Rails def handle_skip @options = @options.merge(stylesheets: false) unless options[:assets] @options = @options.merge(stylesheet_engine: false) unless options[:stylesheets] && options[:scaffold_stylesheet] - @options = @options.merge(system_tests: false) if options[:api] end hook_for :scaffold_controller, required: true - hook_for :system_tests, as: :system - hook_for :assets do |assets| invoke assets, [controller_name] end diff --git a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb index 292db35121..ef2cf3e389 100644 --- a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +++ b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb @@ -11,12 +11,19 @@ module TestUnit # :nodoc: class_option :api, type: :boolean, desc: "Generates API functional tests" + class_option :system_tests, type: :string, + desc: "Skip system test files" + argument :attributes, type: :array, default: [], banner: "field:type field:type" def create_test_files template_file = options.api? ? "api_functional_test.rb" : "functional_test.rb" template template_file, File.join("test/controllers", controller_class_path, "#{controller_file_name}_controller_test.rb") + + unless options.api? || options[:system_tests].nil? + template "system_test.rb", File.join("test/system", class_path, "#{file_name.pluralize}_test.rb") + end end def fixture_name @@ -30,16 +37,20 @@ module TestUnit # :nodoc: private + def attributes_string + attributes_hash.map { |k, v| "#{k}: #{v}" }.join(", ") + end + def attributes_hash - return if attributes_names.empty? + return {} if attributes_names.empty? attributes_names.map do |name| if %w(password password_confirmation).include?(name) && attributes.any?(&:password_digest?) - "#{name}: 'secret'" + ["#{name}", "'secret'"] else - "#{name}: @#{singular_table_name}.#{name}" + ["#{name}", "@#{singular_table_name}.#{name}"] end - end.sort.join(", ") + end.sort.to_h end end 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 c469c188e6..f21861d8e6 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 @@ -17,7 +17,7 @@ class <%= controller_class_name %>ControllerTest < ActionDispatch::IntegrationTe test "should create <%= singular_table_name %>" do assert_difference('<%= class_name %>.count') do - post <%= index_helper %>_url, params: { <%= "#{singular_table_name}: { #{attributes_hash} }" %> }, as: :json + post <%= index_helper %>_url, params: { <%= "#{singular_table_name}: { #{attributes_string} }" %> }, as: :json end assert_response 201 @@ -29,7 +29,7 @@ class <%= controller_class_name %>ControllerTest < ActionDispatch::IntegrationTe end test "should update <%= singular_table_name %>" do - patch <%= show_helper %>, params: { <%= "#{singular_table_name}: { #{attributes_hash} }" %> }, as: :json + patch <%= show_helper %>, params: { <%= "#{singular_table_name}: { #{attributes_string} }" %> }, as: :json assert_response 200 end 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 c33375b7b4..195d60be20 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 @@ -22,7 +22,7 @@ class <%= controller_class_name %>ControllerTest < ActionDispatch::IntegrationTe test "should create <%= singular_table_name %>" do assert_difference('<%= class_name %>.count') do - post <%= index_helper %>_url, params: { <%= "#{singular_table_name}: { #{attributes_hash} }" %> } + post <%= index_helper %>_url, params: { <%= "#{singular_table_name}: { #{attributes_string} }" %> } end assert_redirected_to <%= singular_table_name %>_url(<%= class_name %>.last) @@ -39,7 +39,7 @@ class <%= controller_class_name %>ControllerTest < ActionDispatch::IntegrationTe end test "should update <%= singular_table_name %>" do - patch <%= show_helper %>, params: { <%= "#{singular_table_name}: { #{attributes_hash} }" %> } + patch <%= show_helper %>, params: { <%= "#{singular_table_name}: { #{attributes_string} }" %> } assert_redirected_to <%= singular_table_name %>_url(<%= "@#{singular_table_name}" %>) end diff --git a/railties/lib/rails/generators/test_unit/scaffold/templates/system_test.rb b/railties/lib/rails/generators/test_unit/scaffold/templates/system_test.rb new file mode 100644 index 0000000000..f83f5a5c62 --- /dev/null +++ b/railties/lib/rails/generators/test_unit/scaffold/templates/system_test.rb @@ -0,0 +1,49 @@ +require "application_system_test_case" + +<% module_namespacing do -%> +class <%= class_name.pluralize %>Test < ApplicationSystemTestCase + setup do + @<%= singular_table_name %> = <%= fixture_name %>(:one) + end + + test "visiting the index" do + visit <%= plural_table_name %>_url + assert_selector "h1", text: "<%= class_name.pluralize.titleize %>" + end + + test "creating a <%= human_name %>" do + visit <%= plural_table_name %>_url + click_on "New <%= class_name.titleize %>" + + <%- attributes_hash.each do |attr, value| -%> + fill_in "<%= attr.humanize.titleize %>", with: <%= value %> + <%- end -%> + click_on "Create <%= human_name %>" + + assert_text "<%= human_name %> was successfully created" + click_on "Back" + end + + test "updating a <%= human_name %>" do + visit <%= plural_table_name %>_url + click_on "Edit", match: :first + + <%- attributes_hash.each do |attr, value| -%> + fill_in "<%= attr.humanize.titleize %>", with: <%= value %> + <%- end -%> + click_on "Update <%= human_name %>" + + assert_text "<%= human_name %> was successfully updated" + click_on "Back" + end + + test "destroying a <%= human_name %>" do + visit <%= plural_table_name %>_url + page.accept_confirm do + click_on "Destroy", match: :first + end + + assert_text "<%= human_name %> was successfully destroyed" + end +end +<% end -%> -- cgit v1.2.3