diff options
Diffstat (limited to 'railties/test')
-rw-r--r-- | railties/test/generators/generators_test_helper.rb | 31 | ||||
-rw-r--r-- | railties/test/generators/model_generator_test.rb | 80 |
2 files changed, 66 insertions, 45 deletions
diff --git a/railties/test/generators/generators_test_helper.rb b/railties/test/generators/generators_test_helper.rb index 29ff306551..662646fbff 100644 --- a/railties/test/generators/generators_test_helper.rb +++ b/railties/test/generators/generators_test_helper.rb @@ -36,20 +36,41 @@ class GeneratorsTestCase < Test::Unit::TestCase def assert_file(relative, *contents) absolute = File.join(destination_root, relative) - assert File.exists?(absolute) + assert File.exists?(absolute), "Expected file #{relative.inspect} to exist, but does not" + read = File.read(absolute) unless File.directory?(absolute) contents.each do |content| case content when String - assert_equal content, File.read(absolute) + assert_equal content, read when Regexp - assert_match content, File.read(absolute) + assert_match content, read end end + read end - def assert_no_file(relative, content=nil) + def assert_no_file(relative) absolute = File.join(destination_root, relative) - assert !File.exists?(absolute) + assert !File.exists?(absolute), "Expected file #{relative.inspect} to not exist, but does" + end + + def assert_migration(relative, *contents) + file_name = migration_file_name(relative) + assert file_name, "Expected migration #{relative} to exist, but was not found" + assert_file File.join(File.dirname(relative), file_name), *contents + end + + def assert_no_migration(relative) + file_name = migration_file_name(relative) + assert_nil file_name, "Expected migration #{relative} to not exist, but found #{file_name}" + end + + def migration_file_name(relative) + absolute = File.join(destination_root, relative) + dirname, file_name = File.dirname(absolute), File.basename(absolute).sub(/\.rb$/, '') + + migration = Dir.glob("#{dirname}/[0-9]*_*.rb").grep(/\d+_#{file_name}.rb$/).first + File.basename(migration) if migration end end diff --git a/railties/test/generators/model_generator_test.rb b/railties/test/generators/model_generator_test.rb index 8a9c081cd9..212f0f3691 100644 --- a/railties/test/generators/model_generator_test.rb +++ b/railties/test/generators/model_generator_test.rb @@ -17,28 +17,64 @@ class ModelGeneratorTest < GeneratorsTestCase assert_file "app/models/account.rb", /class Account < ActiveRecord::Base/ end - def test_orm_with_parent_option + def test_model_with_parent_option run_generator ["account", "--parent", "Admin::Account"] assert_file "app/models/account.rb", /class Account < Admin::Account/ end - def test_orm_with_underscored_parent_option + 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", /t\.string :name/, /t\.integer :supplier_id/ + 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"] + content = assert_migration "db/migrate/create_accounts.rb" + assert_no_match /t.timestamps/, content + 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_fixtures_are_skipped + def test_fixture_is_skipped run_generator ["account", "--skip-fixture"] assert_no_file "test/fixtures/accounts.yml" end - def test_fixtures_are_skipped_if_fixture_replacement_is_given + def test_fixture_is_skipped_if_fixture_replacement_is_given content = run_generator ["account", "-r", "fixjour"] assert_match /Could not find and invoke 'fixjour'/, content assert_no_file "test/fixtures/accounts.yml" @@ -49,42 +85,6 @@ class ModelGeneratorTest < GeneratorsTestCase assert_match /The name 'Object' is either already used in your application or reserved/, content end -# def test_model_skip_migration_skips_migration -# run_generator('model', %w(Product name:string --skip-migration)) - -# assert_generated_model_for :product -# assert_generated_fixtures_for :products -# assert_skipped_migration :create_products -# end - -# def test_model_with_attributes_generates_resources_with_attributes -# run_generator('model', %w(Product name:string supplier_id:integer created_at:timestamp)) - -# assert_generated_model_for :product -# assert_generated_fixtures_for :products -# assert_generated_migration :create_products do |t| -# assert_generated_column t, :name, :string -# assert_generated_column t, :supplier_id, :integer -# assert_generated_column t, :created_at, :timestamp -# end -# end - -# def test_model_with_reference_attributes_generates_belongs_to_associations -# run_generator('model', %w(Product name:string supplier:references)) - -# assert_generated_model_for :product do |body| -# assert body =~ /^\s+belongs_to :supplier/, "#{body.inspect} should contain 'belongs_to :supplier'" -# end -# end - -# def test_model_with_belongs_to_attributes_generates_belongs_to_associations -# run_generator('model', %w(Product name:string supplier:belongs_to)) - -# assert_generated_model_for :product do |body| -# assert body =~ /^\s+belongs_to :supplier/, "#{body.inspect} should contain 'belongs_to :supplier'" -# end -# end - protected def run_generator(args=["Account", "name:string", "age:integer"]) |