diff options
Diffstat (limited to 'railties/test/generators/model_generator_test.rb')
-rw-r--r-- | railties/test/generators/model_generator_test.rb | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/railties/test/generators/model_generator_test.rb b/railties/test/generators/model_generator_test.rb new file mode 100644 index 0000000000..62e1c5ab9e --- /dev/null +++ b/railties/test/generators/model_generator_test.rb @@ -0,0 +1,126 @@ +require 'abstract_unit' +require 'generators/generators_test_helper' +require 'generators/rails/model/model_generator' + +class ModelGeneratorTest < GeneratorsTestCase + + def test_help_shows_invoked_generators_options + content = run_generator ["--help"] + assert_match /ActiveRecord options:/, content + assert_match /TestUnit options:/, content + end + + def test_invokes_default_orm + run_generator + assert_file "app/models/account.rb", /class Account < ActiveRecord::Base/ + end + + def test_model_with_parent_option + run_generator ["account", "--parent", "Admin::Account"] + assert_file "app/models/account.rb", /class Account < Admin::Account/ + assert_no_migration "db/migrate/create_accounts.rb" + end + + def test_model_with_underscored_parent_option + run_generator ["account", "--parent", "admin/account"] + assert_file "app/models/account.rb", /class Account < Admin::Account/ + end + + def test_migration + run_generator + assert_migration "db/migrate/create_accounts.rb", /class CreateAccounts < ActiveRecord::Migration/ + end + + def test_migration_is_skipped + run_generator ["account", "--no-migration"] + assert_no_migration "db/migrate/create_accounts.rb" + end + + def test_migration_with_attributes + run_generator ["product", "name:string", "supplier_id:integer"] + + assert_migration "db/migrate/create_products.rb" do |m| + assert_class_method m, :up do |up| + assert_match /create_table :products/, up + assert_match /t\.string :name/, up + assert_match /t\.integer :supplier_id/, up + end + + assert_class_method m, :down do |down| + assert_match /drop_table :products/, down + end + end + end + + def test_model_with_references_attribute_generates_belongs_to_associations + run_generator ["product", "name:string", "supplier_id:references"] + assert_file "app/models/product.rb", /belongs_to :supplier/ + end + + def test_model_with_belongs_to_attribute_generates_belongs_to_associations + run_generator ["product", "name:string", "supplier_id:belongs_to"] + assert_file "app/models/product.rb", /belongs_to :supplier/ + end + + def test_migration_with_timestamps + run_generator + assert_migration "db/migrate/create_accounts.rb", /t.timestamps/ + end + + def test_migration_timestamps_are_skipped + run_generator ["account", "--no-timestamps"] + + assert_migration "db/migrate/create_accounts.rb" do |m| + assert_class_method m, :up do |up| + assert_no_match /t.timestamps/, up + end + end + end + + def test_migration_already_exists_error_message + run_generator + error = capture(:stderr){ run_generator ["Account"], :behavior => :skip } + assert_match /Another migration is already named create_accounts/, error + end + + def test_migration_error_is_not_shown_on_revoke + run_generator + error = capture(:stderr){ run_generator ["Account"], :behavior => :revoke } + assert_no_match /Another migration is already named create_accounts/, error + end + + def test_migration_is_removed_on_revoke + run_generator + run_generator ["Account"], :behavior => :revoke + assert_no_migration "db/migrate/create_accounts.rb" + end + + def test_invokes_default_test_framework + run_generator + assert_file "test/unit/account_test.rb", /class AccountTest < ActiveSupport::TestCase/ + assert_file "test/fixtures/accounts.yml", /name: MyString/, /age: 1/ + end + + def test_fixture_is_skipped + run_generator ["account", "--skip-fixture"] + assert_no_file "test/fixtures/accounts.yml" + end + + def test_fixture_is_skipped_if_fixture_replacement_is_given + content = run_generator ["account", "-r", "factory_girl"] + assert_match /factory_girl \[not found\]/, content + assert_no_file "test/fixtures/accounts.yml" + end + + def test_check_class_collision + content = capture(:stderr){ run_generator ["object"] } + assert_match /The name 'Object' is either already used in your application or reserved/, content + end + + protected + + def run_generator(args=["Account", "name:string", "age:integer"], config={}) + silence(:stdout) { Rails::Generators::ModelGenerator.start args, config.merge(:root => destination_root) } + end + +end |