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/resource/resource_generator "
2009-06-28 13:46:34 -04:00
2010-01-18 18:07:11 -05:00
class ResourceGeneratorTest < Rails :: Generators :: TestCase
include GeneratorsTestHelper
2010-01-03 10:34:32 -05:00
arguments %w( account )
2009-06-28 13:46:34 -04:00
2010-03-19 13:11:37 -04:00
setup :copy_routes
2009-06-28 13:46:34 -04:00
def test_help_with_inherited_options
content = run_generator [ " --help " ]
2011-05-18 07:37:57 -04:00
assert_match ( / ActiveRecord options: / , content )
assert_match ( / TestUnit options: / , content )
2009-06-28 13:46:34 -04:00
end
def test_files_from_inherited_invocation
run_generator
%w(
app / models / account . rb
2012-10-08 00:59:42 -04:00
test / models / account_test . rb
2009-06-28 13:46:34 -04:00
test / fixtures / accounts . yml
) . each { | path | assert_file path }
assert_migration " db/migrate/create_accounts.rb "
end
def test_inherited_invocations_with_attributes
run_generator [ " account " , " name:string " ]
assert_migration " db/migrate/create_accounts.rb " , / t.string :name /
end
def test_resource_controller_with_pluralized_class_name
run_generator
assert_file " app/controllers/accounts_controller.rb " , / class AccountsController < ApplicationController /
2015-10-26 20:41:27 -04:00
assert_file " test/controllers/accounts_controller_test.rb " , / class AccountsControllerTest < ActionDispatch::IntegrationTest /
2009-06-28 13:46:34 -04:00
assert_file " app/helpers/accounts_helper.rb " , / module AccountsHelper /
end
def test_resource_controller_with_actions
run_generator [ " account " , " --actions " , " index " , " new " ]
assert_file " app/controllers/accounts_controller.rb " do | controller |
2010-01-03 09:13:54 -05:00
assert_instance_method :index , controller
assert_instance_method :new , controller
2009-06-28 13:46:34 -04:00
end
assert_file " app/views/accounts/index.html.erb "
assert_file " app/views/accounts/new.html.erb "
end
def test_resource_routes_are_added
run_generator
assert_file " config/routes.rb " do | route |
2011-05-18 07:37:57 -04:00
assert_match ( / resources :accounts$ / , route )
2009-06-28 13:46:34 -04:00
end
end
2009-06-30 02:49:25 -04:00
def test_plural_names_are_singularized
2009-11-03 21:14:44 -05:00
content = run_generator [ " accounts " . freeze ]
2016-06-07 15:41:12 -04:00
assert_file " app/models/account.rb " , / class Account < ApplicationRecord /
2012-10-08 00:59:42 -04:00
assert_file " test/models/account_test.rb " , / class AccountTest /
2014-02-27 11:34:01 -05:00
assert_match ( / \ [WARNING \ ] The model name 'accounts' was recognized as a plural, using the singular 'account' instead \ . Override with --force-plural or setup custom inflection rules for this noun before running the generator \ . / , content )
2009-06-30 02:49:25 -04:00
end
def test_plural_names_can_be_forced
content = run_generator [ " accounts " , " --force-plural " ]
2016-06-07 15:41:12 -04:00
assert_file " app/models/accounts.rb " , / class Accounts < ApplicationRecord /
2012-10-08 00:59:42 -04:00
assert_file " test/models/accounts_test.rb " , / class AccountsTest /
2014-02-27 11:34:01 -05:00
assert_no_match ( / \ [WARNING \ ] / , content )
2009-06-30 02:49:25 -04:00
end
2010-10-14 15:23:19 -04:00
def test_mass_nouns_do_not_throw_warnings
content = run_generator [ " sheep " . freeze ]
2014-02-27 11:34:01 -05:00
assert_no_match ( / \ [WARNING \ ] / , content )
2010-10-14 15:23:19 -04:00
end
2009-07-01 06:07:05 -04:00
def test_route_is_removed_on_revoke
run_generator
2012-10-14 06:03:39 -04:00
run_generator [ " account " ] , behavior : :revoke
2009-07-01 06:07:05 -04:00
assert_file " config/routes.rb " do | route |
2011-05-18 07:37:57 -04:00
assert_no_match ( / resources :accounts$ / , route )
2009-07-01 06:07:05 -04:00
end
end
2009-06-28 13:46:34 -04:00
end