migration_generator_test.rb 1.7 KB
Newer Older
1
require 'generators/generators_test_helper'
2
require 'generators/rails/migration/migration_generator'
3 4 5

class MigrationGeneratorTest < GeneratorsTestCase
  def test_migration
6 7 8
    migration = "change_title_body_from_posts"
    run_generator [migration]
    assert_migration "db/migrate/#{migration}.rb", /class ChangeTitleBodyFromPosts < ActiveRecord::Migration/
9 10 11
  end

  def test_migration_with_class_name
12 13 14
    migration = "ChangeTitleBodyFromPosts"
    run_generator [migration]
    assert_migration "db/migrate/change_title_body_from_posts.rb", /class #{migration} < ActiveRecord::Migration/
15 16 17
  end

  def test_add_migration_with_attributes
18 19
    migration = "add_title_body_to_posts"
    run_generator [migration, "title:string", "body:text"]
20

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

27
      assert_class_method :down, content do |down|
28 29 30
        assert_match /remove_column :posts, :title/, down
        assert_match /remove_column :posts, :body/, down
      end
31 32 33 34
    end
  end

  def test_remove_migration_with_attributes
35 36
    migration = "remove_title_body_from_posts"
    run_generator [migration, "title:string", "body:text"]
37

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

44
      assert_class_method :down, content do |down|
45 46 47
        assert_match /add_column :posts, :title, :string/, down
        assert_match /add_column :posts, :body, :text/, down
      end
48 49 50
    end
  end
end