diff options
Diffstat (limited to 'railties/test/generators')
-rw-r--r-- | railties/test/generators/actions_test.rb | 31 | ||||
-rw-r--r-- | railties/test/generators/app_generator_test.rb | 8 | ||||
-rw-r--r-- | railties/test/generators/generated_attribute_test.rb | 11 | ||||
-rw-r--r-- | railties/test/generators/model_generator_test.rb | 10 | ||||
-rw-r--r-- | railties/test/generators/scaffold_controller_generator_test.rb | 47 | ||||
-rw-r--r-- | railties/test/generators/scaffold_generator_test.rb | 34 |
6 files changed, 85 insertions, 56 deletions
diff --git a/railties/test/generators/actions_test.rb b/railties/test/generators/actions_test.rb index 8af92479c3..54734ed260 100644 --- a/railties/test/generators/actions_test.rb +++ b/railties/test/generators/actions_test.rb @@ -1,8 +1,11 @@ require 'generators/generators_test_helper' require 'rails/generators/rails/app/app_generator' +require 'env_helpers' class ActionsTest < Rails::Generators::TestCase include GeneratorsTestHelper + include EnvHelpers + tests Rails::Generators::AppGenerator arguments [destination_root] @@ -154,10 +157,9 @@ class ActionsTest < Rails::Generators::TestCase def test_rake_should_run_rake_command_with_default_env generator.expects(:run).once.with("rake log:clear RAILS_ENV=development", verbose: false) - old_env, ENV['RAILS_ENV'] = ENV["RAILS_ENV"], nil - action :rake, 'log:clear' - ensure - ENV["RAILS_ENV"] = old_env + with_rails_env nil do + action :rake, 'log:clear' + end end def test_rake_with_env_option_should_run_rake_command_in_env @@ -167,26 +169,23 @@ class ActionsTest < Rails::Generators::TestCase def test_rake_with_rails_env_variable_should_run_rake_command_in_env generator.expects(:run).once.with('rake log:clear RAILS_ENV=production', verbose: false) - old_env, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], "production" - action :rake, 'log:clear' - ensure - ENV["RAILS_ENV"] = old_env + with_rails_env "production" do + action :rake, 'log:clear' + end end def test_env_option_should_win_over_rails_env_variable_when_running_rake generator.expects(:run).once.with('rake log:clear RAILS_ENV=production', verbose: false) - old_env, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], "staging" - action :rake, 'log:clear', env: 'production' - ensure - ENV["RAILS_ENV"] = old_env + with_rails_env "staging" do + action :rake, 'log:clear', env: 'production' + end end def test_rake_with_sudo_option_should_run_rake_command_with_sudo generator.expects(:run).once.with("sudo rake log:clear RAILS_ENV=development", verbose: false) - old_env, ENV['RAILS_ENV'] = ENV["RAILS_ENV"], nil - action :rake, 'log:clear', sudo: true - ensure - ENV["RAILS_ENV"] = old_env + with_rails_env nil do + action :rake, 'log:clear', sudo: true + end end def test_capify_should_run_the_capify_command diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 5ea31f2e0f..b7e366d266 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -55,7 +55,6 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_file "app/views/layouts/application.html.erb", /javascript_include_tag\s+"application"/ assert_file "app/assets/stylesheets/application.css" assert_file "config/application.rb", /config\.assets\.enabled = true/ - assert_file "public/index.html", /url\("assets\/rails.png"\);/ end def test_invalid_application_name_raises_an_error @@ -251,13 +250,6 @@ class AppGeneratorTest < Rails::Generators::TestCase end end - def test_generator_if_skip_index_html_is_given - run_generator [destination_root, '--skip-index-html'] - assert_no_file 'public/index.html' - assert_no_file 'app/assets/images/rails.png' - assert_file 'app/assets/images/.keep' - end - def test_creation_of_a_test_directory run_generator assert_file 'test' diff --git a/railties/test/generators/generated_attribute_test.rb b/railties/test/generators/generated_attribute_test.rb index 6ab1cd58c7..c48bc20899 100644 --- a/railties/test/generators/generated_attribute_test.rb +++ b/railties/test/generators/generated_attribute_test.rb @@ -117,13 +117,13 @@ class GeneratedAttributeTest < Rails::Generators::TestCase assert create_generated_attribute("#{attribute_type}{polymorphic}").polymorphic? end end - + def test_polymorphic_reference_is_false %w(foo bar baz).each do |attribute_type| assert !create_generated_attribute("#{attribute_type}{polymorphic}").polymorphic? end end - + def test_blank_type_defaults_to_string_raises_exception assert_equal :string, create_generated_attribute(nil, 'title').type assert_equal :string, create_generated_attribute("", 'title').type @@ -132,6 +132,13 @@ class GeneratedAttributeTest < Rails::Generators::TestCase def test_handles_index_names_for_references assert_equal "post", create_generated_attribute('string', 'post').index_name assert_equal "post_id", create_generated_attribute('references', 'post').index_name + assert_equal "post_id", create_generated_attribute('belongs_to', 'post').index_name assert_equal ["post_id", "post_type"], create_generated_attribute('references{polymorphic}', 'post').index_name end + + def test_handles_column_names_for_references + assert_equal "post", create_generated_attribute('string', 'post').column_name + assert_equal "post_id", create_generated_attribute('references', 'post').column_name + assert_equal "post_id", create_generated_attribute('belongs_to', 'post').column_name + end end diff --git a/railties/test/generators/model_generator_test.rb b/railties/test/generators/model_generator_test.rb index 0c7ff0ebe7..70e080a8ab 100644 --- a/railties/test/generators/model_generator_test.rb +++ b/railties/test/generators/model_generator_test.rb @@ -273,6 +273,16 @@ class ModelGeneratorTest < Rails::Generators::TestCase assert_file "test/fixtures/accounts.yml", /name: MyString/, /age: 1/ end + def test_fixtures_use_the_references_ids + run_generator ["LineItem", "product:references", "cart:belongs_to"] + assert_file "test/fixtures/line_items.yml", /product_id: \n cart_id: / + end + + def test_fixtures_use_the_references_ids_and_type + run_generator ["LineItem", "product:references{polymorphic}", "cart:belongs_to"] + assert_file "test/fixtures/line_items.yml", /product_id: \n product_type: Product\n cart_id: / + end + def test_fixture_is_skipped run_generator ["account", "--skip-fixture"] assert_no_file "test/fixtures/accounts.yml" diff --git a/railties/test/generators/scaffold_controller_generator_test.rb b/railties/test/generators/scaffold_controller_generator_test.rb index 8cacca668f..57a12d0457 100644 --- a/railties/test/generators/scaffold_controller_generator_test.rb +++ b/railties/test/generators/scaffold_controller_generator_test.rb @@ -20,17 +20,13 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase assert_match(/@users = User\.all/, m) end - assert_instance_method :show, content do |m| - assert_match(/@user = User\.find\(params\[:id\]\)/, m) - end + assert_instance_method :show, content assert_instance_method :new, content do |m| assert_match(/@user = User\.new/, m) end - assert_instance_method :edit, content do |m| - assert_match(/@user = User\.find\(params\[:id\]\)/, m) - end + assert_instance_method :edit, content assert_instance_method :create, content do |m| assert_match(/@user = User\.new\(user_params\)/, m) @@ -39,21 +35,50 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase end assert_instance_method :update, content do |m| - assert_match(/@user = User\.find\(params\[:id\]\)/, m) assert_match(/@user\.update_attributes\(user_params\)/, m) assert_match(/@user\.errors/, m) end assert_instance_method :destroy, content do |m| - assert_match(/@user = User\.find\(params\[:id\]\)/, m) assert_match(/@user\.destroy/, m) end + assert_instance_method :set_user, content do |m| + assert_match(/@user = User\.find\(params\[:id\]\)/, m) + end + assert_match(/def user_params/, content) assert_match(/params\.require\(:user\)\.permit\(:name, :age\)/, content) end end + def test_dont_use_require_or_permit_if_there_are_no_attributes + run_generator ["User"] + + assert_file "app/controllers/users_controller.rb" do |content| + assert_match(/def user_params/, content) + assert_match(/params\[:user\]/, content) + end + end + + def test_controller_permit_references_attributes + run_generator ["LineItem", "product:references", "cart:belongs_to"] + + assert_file "app/controllers/line_items_controller.rb" do |content| + assert_match(/def line_item_params/, content) + assert_match(/params\.require\(:line_item\)\.permit\(:product_id, :cart_id\)/, content) + end + end + + def test_controller_permit_polymorphic_references_attributes + run_generator ["LineItem", "product:references{polymorphic}"] + + assert_file "app/controllers/line_items_controller.rb" do |content| + assert_match(/def line_item_params/, content) + assert_match(/params\.require\(:line_item\)\.permit\(:product_id, :product_type\)/, content) + end + end + def test_helper_are_invoked_with_a_pluralized_name run_generator assert_file "app/helpers/users_helper.rb", /module UsersHelper/ @@ -70,13 +95,13 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase end def test_functional_tests - run_generator + run_generator ["User", "name:string", "age:integer", "organization:references{polymorphic}"] assert_file "test/controllers/users_controller_test.rb" do |content| assert_match(/class UsersControllerTest < ActionController::TestCase/, content) assert_match(/test "should get index"/, content) - assert_match(/post :create, user: \{ age: @user.age, name: @user.name \}/, content) - assert_match(/put :update, id: @user, user: \{ age: @user.age, name: @user.name \}/, content) + assert_match(/post :create, user: \{ age: @user\.age, name: @user\.name, organization_id: @user\.organization_id, organization_type: @user\.organization_type \}/, content) + assert_match(/put :update, id: @user, user: \{ age: @user\.age, name: @user\.name, organization_id: @user\.organization_id, organization_type: @user\.organization_type \}/, content) end end diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb index 54d5a9db6f..de62fdb1ea 100644 --- a/railties/test/generators/scaffold_generator_test.rb +++ b/railties/test/generators/scaffold_generator_test.rb @@ -30,17 +30,13 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase assert_match(/@product_lines = ProductLine\.all/, m) end - assert_instance_method :show, content do |m| - assert_match(/@product_line = ProductLine\.find\(params\[:id\]\)/, m) - end + assert_instance_method :show, content assert_instance_method :new, content do |m| assert_match(/@product_line = ProductLine\.new/, m) end - assert_instance_method :edit, content do |m| - assert_match(/@product_line = ProductLine\.find\(params\[:id\]\)/, m) - end + assert_instance_method :edit, content assert_instance_method :create, content do |m| assert_match(/@product_line = ProductLine\.new\(product_line_params\)/, m) @@ -49,21 +45,23 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase end assert_instance_method :update, content do |m| - assert_match(/@product_line = ProductLine\.find\(params\[:id\]\)/, m) assert_match(/@product_line\.update_attributes\(product_line_params\)/, m) assert_match(/@product_line\.errors/, m) end assert_instance_method :destroy, content do |m| - assert_match(/@product_line = ProductLine\.find\(params\[:id\]\)/, m) assert_match(/@product_line\.destroy/, m) end + + assert_instance_method :set_product_line, content do |m| + assert_match(/@product_line = ProductLine\.find\(params\[:id\]\)/, m) + end end assert_file "test/controllers/product_lines_controller_test.rb" do |test| assert_match(/class ProductLinesControllerTest < ActionController::TestCase/, test) - assert_match(/post :create, product_line: \{ title: @product_line.title \}/, test) - assert_match(/put :update, id: @product_line, product_line: \{ title: @product_line.title \}/, test) + assert_match(/post :create, product_line: \{ product_id: @product_line\.product_id, title: @product_line\.title, user_id: @product_line\.user_id \}/, test) + assert_match(/put :update, id: @product_line, product_line: \{ product_id: @product_line\.product_id, title: @product_line\.title, user_id: @product_line\.user_id \}/, test) end # Views @@ -149,17 +147,13 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase assert_match(/@admin_roles = Admin::Role\.all/, m) end - assert_instance_method :show, content do |m| - assert_match(/@admin_role = Admin::Role\.find\(params\[:id\]\)/, m) - end + assert_instance_method :show, content assert_instance_method :new, content do |m| assert_match(/@admin_role = Admin::Role\.new/, m) end - assert_instance_method :edit, content do |m| - assert_match(/@admin_role = Admin::Role\.find\(params\[:id\]\)/, m) - end + assert_instance_method :edit, content assert_instance_method :create, content do |m| assert_match(/@admin_role = Admin::Role\.new\(admin_role_params\)/, m) @@ -168,15 +162,17 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase end assert_instance_method :update, content do |m| - assert_match(/@admin_role = Admin::Role\.find\(params\[:id\]\)/, m) assert_match(/@admin_role\.update_attributes\(admin_role_params\)/, m) assert_match(/@admin_role\.errors/, m) end assert_instance_method :destroy, content do |m| - assert_match(/@admin_role = Admin::Role\.find\(params\[:id\]\)/, m) assert_match(/@admin_role\.destroy/, m) end + + assert_instance_method :set_admin_role, content do |m| + assert_match(/@admin_role = Admin::Role\.find\(params\[:id\]\)/, m) + end end assert_file "test/controllers/admin/roles_controller_test.rb", @@ -203,7 +199,7 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase run_generator [ "admin/role" ], :behavior => :revoke # Model - assert_file "app/models/admin.rb" # ( should not be remove ) + assert_file "app/models/admin.rb" # ( should not be remove ) assert_no_file "app/models/admin/role.rb" assert_no_file "test/models/admin/role_test.rb" assert_no_file "test/fixtures/admin/roles.yml" |