2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
require "generators/generators_test_helper"
|
|
|
|
require "rails/generators/rails/controller/controller_generator"
|
2009-06-26 05:57:40 -04:00
|
|
|
|
2010-01-18 18:07:11 -05:00
|
|
|
class ControllerGeneratorTest < Rails::Generators::TestCase
|
|
|
|
include GeneratorsTestHelper
|
2010-01-03 10:34:32 -05:00
|
|
|
arguments %w(Account foo bar)
|
2009-06-26 05:57:40 -04:00
|
|
|
|
2010-03-19 13:11:37 -04:00
|
|
|
setup :copy_routes
|
|
|
|
|
2009-06-27 15:26:53 -04:00
|
|
|
def test_help_does_not_show_invoked_generators_options_if_they_already_exist
|
|
|
|
content = run_generator ["--help"]
|
2020-09-07 07:13:53 -04:00
|
|
|
assert_no_match(/Helper options:/, content)
|
2009-06-27 15:26:53 -04:00
|
|
|
end
|
|
|
|
|
2009-06-26 05:57:40 -04:00
|
|
|
def test_controller_skeleton_is_created
|
|
|
|
run_generator
|
|
|
|
assert_file "app/controllers/account_controller.rb", /class AccountController < ApplicationController/
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_check_class_collision
|
2020-09-14 21:18:40 -04:00
|
|
|
Object.const_set :ObjectController, Class.new
|
2016-08-16 03:30:11 -04:00
|
|
|
content = capture(:stderr) { run_generator ["object"] }
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/The name 'ObjectController' is either already used in your application or reserved/, content)
|
2009-07-03 06:10:09 -04:00
|
|
|
ensure
|
|
|
|
Object.send :remove_const, :ObjectController
|
2009-06-26 05:57:40 -04:00
|
|
|
end
|
|
|
|
|
2009-06-26 14:42:00 -04:00
|
|
|
def test_invokes_helper
|
2009-06-26 05:57:40 -04:00
|
|
|
run_generator
|
|
|
|
assert_file "app/helpers/account_helper.rb"
|
|
|
|
end
|
|
|
|
|
2009-06-26 14:42:00 -04:00
|
|
|
def test_does_not_invoke_helper_if_required
|
|
|
|
run_generator ["account", "--skip-helper"]
|
|
|
|
assert_no_file "app/helpers/account_helper.rb"
|
|
|
|
end
|
|
|
|
|
2011-04-14 06:23:21 -04:00
|
|
|
def test_invokes_assets
|
|
|
|
run_generator
|
2011-05-24 02:39:04 -04:00
|
|
|
assert_file "app/assets/stylesheets/account.css"
|
2011-04-14 06:23:21 -04:00
|
|
|
end
|
|
|
|
|
2013-11-23 11:17:18 -05:00
|
|
|
def test_does_not_invoke_assets_if_required
|
|
|
|
run_generator ["account", "--skip-assets"]
|
|
|
|
assert_no_file "app/assets/stylesheets/account.css"
|
|
|
|
end
|
|
|
|
|
2009-06-26 05:57:40 -04:00
|
|
|
def test_invokes_default_test_framework
|
|
|
|
run_generator
|
2012-10-08 00:59:42 -04:00
|
|
|
assert_file "test/controllers/account_controller_test.rb"
|
2009-06-26 05:57:40 -04:00
|
|
|
end
|
|
|
|
|
2009-06-26 14:42:00 -04:00
|
|
|
def test_does_not_invoke_test_framework_if_required
|
|
|
|
run_generator ["account", "--no-test-framework"]
|
2012-10-08 00:59:42 -04:00
|
|
|
assert_no_file "test/controllers/account_controller_test.rb"
|
2009-06-26 14:42:00 -04:00
|
|
|
end
|
|
|
|
|
2009-06-26 05:57:40 -04:00
|
|
|
def test_invokes_default_template_engine
|
|
|
|
run_generator
|
2010-03-21 19:05:35 -04:00
|
|
|
assert_file "app/views/account/foo.html.erb", %r(app/views/account/foo\.html\.erb)
|
|
|
|
assert_file "app/views/account/bar.html.erb", %r(app/views/account/bar\.html\.erb)
|
2009-06-26 05:57:40 -04:00
|
|
|
end
|
|
|
|
|
2010-03-19 13:11:37 -04:00
|
|
|
def test_add_routes
|
|
|
|
run_generator
|
2016-12-25 01:14:39 -05:00
|
|
|
assert_file "config/routes.rb", /^ get 'account\/foo'/, /^ get 'account\/bar'/
|
2010-03-19 13:11:37 -04:00
|
|
|
end
|
|
|
|
|
2014-06-19 17:42:52 -04:00
|
|
|
def test_skip_routes
|
|
|
|
run_generator ["account", "foo", "--skip-routes"]
|
|
|
|
assert_file "config/routes.rb" do |routes|
|
2014-07-04 10:18:34 -04:00
|
|
|
assert_no_match(/get 'account\/foo'/, routes)
|
2014-06-19 17:42:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-22 16:41:41 -05:00
|
|
|
def test_skip_routes_prevents_generating_tests_with_routes
|
|
|
|
run_generator ["account", "foo", "--skip-routes"]
|
|
|
|
assert_file "test/controllers/account_controller_test.rb" do |controller_test|
|
|
|
|
assert_no_match(/account_foo_(url|path)/, controller_test)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-06-26 05:57:40 -04:00
|
|
|
def test_invokes_default_template_engine_even_with_no_action
|
|
|
|
run_generator ["account"]
|
|
|
|
assert_file "app/views/account"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_template_engine_with_class_path
|
|
|
|
run_generator ["admin/account"]
|
|
|
|
assert_file "app/views/admin/account"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_actions_are_turned_into_methods
|
|
|
|
run_generator
|
2009-06-28 13:46:34 -04:00
|
|
|
|
|
|
|
assert_file "app/controllers/account_controller.rb" do |controller|
|
2010-01-03 09:13:54 -05:00
|
|
|
assert_instance_method :foo, controller
|
|
|
|
assert_instance_method :bar, controller
|
2009-06-28 13:46:34 -04:00
|
|
|
end
|
2009-06-26 05:57:40 -04:00
|
|
|
end
|
2013-07-21 11:06:35 -04:00
|
|
|
|
|
|
|
def test_namespaced_routes_are_created_in_routes
|
|
|
|
run_generator ["admin/dashboard", "index"]
|
2015-04-13 19:58:21 -04:00
|
|
|
assert_file "config/routes.rb" do |route|
|
|
|
|
assert_match(/^ namespace :admin do\n get 'dashboard\/index'\n end$/, route)
|
|
|
|
end
|
2013-07-21 11:06:35 -04:00
|
|
|
end
|
2017-08-13 19:12:56 -04:00
|
|
|
|
|
|
|
def test_namespaced_routes_with_multiple_actions_are_created_in_routes
|
|
|
|
run_generator ["admin/dashboard", "index", "show"]
|
|
|
|
assert_file "config/routes.rb" do |route|
|
|
|
|
assert_match(/^ namespace :admin do\n get 'dashboard\/index'\n get 'dashboard\/show'\n end$/, route)
|
|
|
|
end
|
|
|
|
end
|
2018-02-21 18:39:37 -05:00
|
|
|
|
|
|
|
def test_does_not_add_routes_when_action_is_not_specified
|
|
|
|
run_generator ["admin/dashboard"]
|
|
|
|
assert_file "config/routes.rb" do |routes|
|
|
|
|
assert_no_match(/namespace :admin/, routes)
|
|
|
|
end
|
|
|
|
end
|
2017-12-23 21:19:33 -05:00
|
|
|
|
|
|
|
def test_controller_suffix_is_not_duplicated
|
|
|
|
run_generator ["account_controller"]
|
|
|
|
|
|
|
|
assert_no_file "app/controllers/account_controller_controller.rb"
|
|
|
|
assert_file "app/controllers/account_controller.rb"
|
|
|
|
|
|
|
|
assert_no_file "app/views/account_controller/"
|
|
|
|
assert_file "app/views/account/"
|
|
|
|
|
|
|
|
assert_no_file "test/controllers/account_controller_controller_test.rb"
|
|
|
|
assert_file "test/controllers/account_controller_test.rb"
|
|
|
|
|
|
|
|
assert_no_file "app/helpers/account_controller_helper.rb"
|
|
|
|
assert_file "app/helpers/account_helper.rb"
|
|
|
|
|
|
|
|
assert_no_file "app/assets/stylesheets/account_controller.css"
|
|
|
|
assert_file "app/assets/stylesheets/account.css"
|
|
|
|
end
|
2009-06-26 05:57:40 -04:00
|
|
|
end
|