migration_generator_test.rb 1.9 KB
Newer Older
1 2
require 'abstract_unit'
require 'generators/generators_test_helper'
3
require 'rails/generators/rails/migration/migration_generator'
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

class MigrationGeneratorTest < GeneratorsTestCase

  def test_migration
    @migration = "change_title_body_from_posts"
    run_generator
    assert_migration "db/migrate/#{@migration}.rb", /class ChangeTitleBodyFromPosts < ActiveRecord::Migration/
  end

  def test_migration_with_class_name
    @migration = "ChangeTitleBodyFromPosts"
    run_generator
    assert_migration "db/migrate/change_title_body_from_posts.rb", /class #{@migration} < ActiveRecord::Migration/
  end

  def test_add_migration_with_attributes
    @migration = "add_title_body_to_posts"
    run_generator [@migration, "title:string", "body:text"]

23 24 25 26 27
    assert_migration "db/migrate/#{@migration}.rb" do |content|
      assert_class_method content, :up do |up|
        assert_match /add_column :posts, :title, :string/, up
        assert_match /add_column :posts, :body, :text/, up
      end
28

29 30 31 32
      assert_class_method content, :down do |down|
        assert_match /remove_column :posts, :title/, down
        assert_match /remove_column :posts, :body/, down
      end
33 34 35 36 37 38 39
    end
  end

  def test_remove_migration_with_attributes
    @migration = "remove_title_body_from_posts"
    run_generator [@migration, "title:string", "body:text"]

40 41 42 43 44
    assert_migration "db/migrate/#{@migration}.rb" do |content|
      assert_class_method content, :up do |up|
        assert_match /remove_column :posts, :title/, up
        assert_match /remove_column :posts, :body/, up
      end
45

46 47 48 49
      assert_class_method content, :down do |down|
        assert_match /add_column :posts, :title, :string/, down
        assert_match /add_column :posts, :body, :text/, down
      end
50 51 52 53 54 55
    end
  end

  protected

    def run_generator(args=[@migration])
Y
Yehuda Katz 已提交
56
      silence(:stdout) { Rails::Generators::MigrationGenerator.start args, :destination_root => destination_root }
57 58 59
    end

end