Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #11487: fix_generator_tests.diff

File fix_generator_tests.diff, 19.5 kB (added by thechrisoshow, 8 months ago)

Removed protected method that was never used

  • railties/test/rails_generator_test.rb

    old new  
    3333end 
    3434 
    3535$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib" 
    36 require 'rails_generator' 
     36require 'generators/generator_test_helper' 
    3737 
    38  
    3938class RailsGeneratorTest < Test::Unit::TestCase 
    4039  BUILTINS = %w(controller integration_test mailer migration model observer plugin resource scaffold session_migration) 
    4140  CAPITALIZED_BUILTINS = BUILTINS.map { |b| b.capitalize } 
     
    4645 
    4746  def test_sources 
    4847    expected = [:lib, :vendor,  
    49                 :plugins, :plugins, # <plugin>/generators and <plugin>/rails_generators 
     48                "plugins (vendor/plugins)".to_sym, # <plugin>/generators and <plugin>/rails_generators 
    5049                :user,  
    5150                :RubyGems, :RubyGems, # gems named <x>_generator, gems containing /rails_generator/ folder 
    5251                :builtin] 
     
    9897  def test_generator_spec 
    9998    spec = Rails::Generator::Base.lookup('working') 
    10099    assert_equal 'working', spec.name 
    101     assert_equal "#{RAILS_ROOT}/lib/generators/working", spec.path 
     100    assert_match(/#{spec.path}$/, "#{RAILS_ROOT}/lib/generators/working") 
    102101    assert_equal :lib, spec.source 
    103102    assert_nothing_raised { assert_match(/WorkingGenerator$/, spec.klass.name) } 
    104103  end 
  • railties/test/generators/rails_model_generator_test.rb

    old new  
    4848end 
    4949Dir.mkdir(RAILS_ROOT) unless File.exist?(RAILS_ROOT) 
    5050 
     51 
    5152$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib" 
    52 require 'rails_generator' 
     53 
    5354require 'generators/generator_test_helper' 
    5455 
    5556class RailsModelGeneratorTest < Test::Unit::TestCase 
     
    8081  end 
    8182 
    8283  def test_model_generates_resources 
    83     run_generator('model', %w(Product)) 
     84    run_generator('model', %w(Product name:string)) 
    8485 
    8586    assert_generated_model_for :product 
    8687    assert_generated_fixtures_for :products 
     
    8889  end 
    8990 
    9091  def test_model_skip_migration_skips_migration 
    91     run_generator('model', %w(Product --skip-migration)) 
     92    run_generator('model', %w(Product name:string --skip-migration)) 
    9293 
    9394    assert_generated_model_for :product 
    9495    assert_generated_fixtures_for :products 
  • railties/test/generators/rails_scaffold_generator_test.rb

    old new  
    1 require 'test/unit' 
     1require 'abstract_unit' 
    22 
    33# Optionally load RubyGems. 
    44begin 
     
    5151Dir.mkdir(RAILS_ROOT) unless File.exist?(RAILS_ROOT) 
    5252 
    5353$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib" 
    54 require 'rails_generator' 
     54 
    5555require 'generators/generator_test_helper' 
    5656 
    57 class RailsScaffoldGeneratorTest < Test::Unit::TestCase 
     57uses_mocha "Scaffold Generator Tests" do 
     58  class RailsScaffoldGeneratorTest < Test::Unit::TestCase 
    5859 
    59   include GeneratorTestHelper 
     60    include GeneratorTestHelper 
    6061 
    61   def setup 
    62     ActiveRecord::Base.pluralize_table_names = true 
    63     Dir.mkdir("#{RAILS_ROOT}/app") unless File.exist?("#{RAILS_ROOT}/app") 
    64     Dir.mkdir("#{RAILS_ROOT}/app/views") unless File.exist?("#{RAILS_ROOT}/app/views") 
    65     Dir.mkdir("#{RAILS_ROOT}/app/views/layouts") unless File.exist?("#{RAILS_ROOT}/app/views/layouts") 
    66     Dir.mkdir("#{RAILS_ROOT}/config") unless File.exist?("#{RAILS_ROOT}/config") 
    67     Dir.mkdir("#{RAILS_ROOT}/db") unless File.exist?("#{RAILS_ROOT}/db") 
    68     Dir.mkdir("#{RAILS_ROOT}/test") unless File.exist?("#{RAILS_ROOT}/test") 
    69     Dir.mkdir("#{RAILS_ROOT}/test/fixtures") unless File.exist?("#{RAILS_ROOT}/test/fixtures") 
    70     Dir.mkdir("#{RAILS_ROOT}/public") unless File.exist?("#{RAILS_ROOT}/public") 
    71     Dir.mkdir("#{RAILS_ROOT}/public/stylesheets") unless File.exist?("#{RAILS_ROOT}/public/stylesheets") 
    72     File.open("#{RAILS_ROOT}/config/routes.rb", 'w') do |f| 
    73       f<<"ActionController::Routing::Routes.draw do |map|\n\nend\n" 
     62    def setup 
     63      ActiveRecord::Base.pluralize_table_names = true 
     64      Dir.mkdir("#{RAILS_ROOT}/app") unless File.exist?("#{RAILS_ROOT}/app") 
     65      Dir.mkdir("#{RAILS_ROOT}/app/views") unless File.exist?("#{RAILS_ROOT}/app/views") 
     66      Dir.mkdir("#{RAILS_ROOT}/app/views/layouts") unless File.exist?("#{RAILS_ROOT}/app/views/layouts") 
     67      Dir.mkdir("#{RAILS_ROOT}/config") unless File.exist?("#{RAILS_ROOT}/config") 
     68      Dir.mkdir("#{RAILS_ROOT}/db") unless File.exist?("#{RAILS_ROOT}/db") 
     69      Dir.mkdir("#{RAILS_ROOT}/test") unless File.exist?("#{RAILS_ROOT}/test") 
     70      Dir.mkdir("#{RAILS_ROOT}/test/fixtures") unless File.exist?("#{RAILS_ROOT}/test/fixtures") 
     71      Dir.mkdir("#{RAILS_ROOT}/public") unless File.exist?("#{RAILS_ROOT}/public") 
     72      Dir.mkdir("#{RAILS_ROOT}/public/stylesheets") unless File.exist?("#{RAILS_ROOT}/public/stylesheets") 
     73      File.open("#{RAILS_ROOT}/config/routes.rb", 'w') do |f| 
     74        f<<"ActionController::Routing::Routes.draw do |map|\n\nend\n" 
     75      end 
    7476    end 
    75   end 
    7677 
    77   def teardown 
    78     FileUtils.rm_rf "#{RAILS_ROOT}/app" 
    79     FileUtils.rm_rf "#{RAILS_ROOT}/test" 
    80     FileUtils.rm_rf "#{RAILS_ROOT}/config" 
    81     FileUtils.rm_rf "#{RAILS_ROOT}/db" 
    82     FileUtils.rm_rf "#{RAILS_ROOT}/public" 
    83   end 
     78    def teardown 
     79      FileUtils.rm_rf "#{RAILS_ROOT}/app" 
     80      FileUtils.rm_rf "#{RAILS_ROOT}/test" 
     81      FileUtils.rm_rf "#{RAILS_ROOT}/config" 
     82      FileUtils.rm_rf "#{RAILS_ROOT}/db" 
     83      FileUtils.rm_rf "#{RAILS_ROOT}/public" 
     84    end 
    8485 
    85   def test_scaffolded_names 
    86     g = Rails::Generator::Base.instance('scaffold', %w(ProductLine)) 
    87     assert_equal "ProductLines", g.controller_name 
    88     assert_equal "ProductLines", g.controller_class_name 
    89     assert_equal "ProductLine", g.controller_singular_name 
    90     assert_equal "product_lines", g.controller_plural_name 
    91     assert_equal "product_lines", g.controller_file_name 
    92     assert_equal "product_lines", g.controller_table_name 
    93   end 
     86    def test_scaffolded_names 
     87      g = Rails::Generator::Base.instance('scaffold', %w(ProductLine)) 
     88      assert_equal "ProductLines", g.controller_name 
     89      assert_equal "ProductLines", g.controller_class_name 
     90      assert_equal "ProductLine", g.controller_singular_name 
     91      assert_equal "product_lines", g.controller_plural_name 
     92      assert_equal "product_lines", g.controller_file_name 
     93      assert_equal "product_lines", g.controller_table_name 
     94    end 
    9495 
    95   def test_scaffold_generates_resources 
     96    def test_scaffold_generates_resources 
    9697 
    97     run_generator('scaffold', %w(Product)) 
     98      run_generator('scaffold', %w(Product name:string)) 
    9899 
    99     assert_generated_controller_for :products do |f| 
     100      assert_generated_controller_for :products do |f| 
    100101 
    101       assert_has_method f, :index do |name, m| 
    102         assert_match /@products = Product\.find\(:all\)/, m, "#{name} should query products table" 
    103       end 
     102        assert_has_method f, :index do |name, m| 
     103          assert_match /@products = Product\.find\(:all\)/, m, "#{name} should query products table" 
     104        end 
    104105 
    105       assert_has_method f, :show, :edit, :update, :destroy do |name, m| 
    106         assert_match /@product = Product\.find\(params\[:id\]\)/, m, "#{name.to_s} should query products table" 
    107       end 
     106        assert_has_method f, :show, :edit, :update, :destroy do |name, m| 
     107          assert_match /@product = Product\.find\(params\[:id\]\)/, m, "#{name.to_s} should query products table" 
     108        end 
    108109 
    109       assert_has_method f, :new do |name, m| 
    110         assert_match /@product = Product\.new/, m, "#{name.to_s} should instantiate a product" 
    111       end 
     110        assert_has_method f, :new do |name, m| 
     111          assert_match /@product = Product\.new/, m, "#{name.to_s} should instantiate a product" 
     112        end 
    112113 
    113       assert_has_method f, :create do |name, m| 
    114         assert_match /@product = Product\.new\(params\[:product\]\)/, m, "#{name.to_s} should instantiate a product" 
    115         assert_match /format.xml  \{ render :xml => @product.errors, :status => :unprocessable_entity \}/, m, "#{name.to_s} should set status to :unprocessable_entity code for xml" 
     114        assert_has_method f, :create do |name, m| 
     115          assert_match /@product = Product\.new\(params\[:product\]\)/, m, "#{name.to_s} should instantiate a product" 
     116          assert_match /format.xml  \{ render :xml => @product.errors, :status => :unprocessable_entity \}/, m, "#{name.to_s} should set status to :unprocessable_entity code for xml" 
     117        end 
     118 
    116119      end 
    117120 
     121      assert_generated_model_for :product 
     122      assert_generated_functional_test_for :products 
     123      assert_generated_unit_test_for :product 
     124      assert_generated_fixtures_for :products 
     125      assert_generated_helper_for :products 
     126      assert_generated_stylesheet :scaffold 
     127      assert_generated_views_for :products, "index.html.erb", "new.html.erb", "edit.html.erb", "show.html.erb" 
     128       
     129      assert_generated_migration :create_products 
     130      assert_added_route_for :products 
    118131    end 
    119132 
    120     assert_generated_model_for :product 
    121     assert_generated_functional_test_for :products 
    122     assert_generated_unit_test_for :product 
    123     assert_generated_fixtures_for :products 
    124     assert_generated_helper_for :products 
    125     assert_generated_stylesheet :scaffold 
    126     assert_generated_views_for :products, "index.html.erb", "new.html.erb", "edit.html.erb", "show.html.erb" 
    127     assert_generated_migration :create_products 
    128     assert_added_route_for :products 
    129   end 
     133    def test_scaffold_skip_migration_skips_migration 
     134      run_generator('scaffold', %w(Product name:string --skip-migration)) 
    130135 
    131   def test_scaffold_skip_migration_skips_migration 
    132     run_generator('scaffold', %w(Product --skip-migration)) 
     136      assert_generated_model_for :product 
     137      assert_generated_functional_test_for :products 
     138      assert_generated_unit_test_for :product 
     139      assert_generated_fixtures_for :products 
     140      assert_generated_helper_for :products 
     141      assert_generated_stylesheet :scaffold 
     142      assert_generated_views_for :products, "index.html.erb","new.html.erb","edit.html.erb","show.html.erb" 
     143      assert_skipped_migration :create_products 
     144      assert_added_route_for :products 
     145    end 
    133146 
    134     assert_generated_model_for :product 
    135     assert_generated_functional_test_for :products 
    136     assert_generated_unit_test_for :product 
    137     assert_generated_fixtures_for :products 
    138     assert_generated_helper_for :products 
    139     assert_generated_stylesheet :scaffold 
    140     assert_generated_views_for :products, "index.html.erb","new.html.erb","edit.html.erb","show.html.erb" 
    141     assert_skipped_migration :create_products 
    142     assert_added_route_for :products 
    143   end 
     147    def test_scaffold_generates_resources_with_attributes 
     148      run_generator('scaffold', %w(Product name:string supplier_id:integer created_at:timestamp)) 
    144149 
    145   def test_scaffold_generates_resources_with_attributes 
    146     run_generator('scaffold', %w(Product name:string supplier_id:integer created_at:timestamp)) 
     150      assert_generated_controller_for :products do |f| 
    147151 
    148     assert_generated_controller_for :products do |f| 
     152        assert_has_method f, :index do |name, m| 
     153          assert_match /@products = Product\.find\(:all\)/, m, "#{name} should query products table" 
     154        end 
    149155 
    150       assert_has_method f, :index do |name, m| 
    151         assert_match /@products = Product\.find\(:all\)/, m, "#{name} should query products table" 
    152       end 
     156        assert_has_method f, :show, :edit, :update, :destroy do |name, m| 
     157          assert_match /@product = Product\.find\(params\[:id\]\)/, m, "#{name.to_s} should query products table" 
     158        end 
    153159 
    154       assert_has_method f, :show, :edit, :update, :destroy do |name, m| 
    155         assert_match /@product = Product\.find\(params\[:id\]\)/, m, "#{name.to_s} should query products table
    156       end 
     160        assert_has_method f, :new do |name, m| 
     161          assert_match /@product = Product\.new/, m, "#{name.to_s} should instantiate a product
     162        end 
    157163 
    158       assert_has_method f, :new do |name, m| 
    159         assert_match /@product = Product\.new/, m, "#{name.to_s} should instantiate a product" 
     164        assert_has_method f, :create do |name, m| 
     165          assert_match /@product = Product\.new\(params\[:product\]\)/, m, "#{name.to_s} should instantiate a product" 
     166          assert_match /format.xml  \{ render :xml => @product.errors, :status => :unprocessable_entity \}/, m, "#{name.to_s} should set status to :unprocessable_entity code for xml" 
     167        end 
     168 
    160169      end 
    161170 
    162       assert_has_method f, :create do |name, m| 
    163         assert_match /@product = Product\.new\(params\[:product\]\)/, m, "#{name.to_s} should instantiate a product" 
    164         assert_match /format.xml  \{ render :xml => @product.errors, :status => :unprocessable_entity \}/, m, "#{name.to_s} should set status to :unprocessable_entity code for xml" 
     171      assert_generated_model_for :product 
     172      assert_generated_functional_test_for :products 
     173      assert_generated_unit_test_for :product 
     174      assert_generated_fixtures_for :products 
     175      assert_generated_helper_for :products 
     176      assert_generated_stylesheet :scaffold 
     177      assert_generated_views_for :products, "index.html.erb", "new.html.erb", "edit.html.erb", "show.html.erb" 
     178     
     179      assert_generated_migration :create_products do |t| 
     180        assert_generated_column t, :name, :string 
     181        assert_generated_column t, :supplier_id, :integer 
     182        assert_generated_column t, :created_at, :timestamp 
    165183      end 
    166184 
     185      assert_added_route_for :products 
    167186    end 
    168187 
    169     assert_generated_model_for :product 
    170     assert_generated_functional_test_for :products 
    171     assert_generated_unit_test_for :product 
    172     assert_generated_fixtures_for :products 
    173     assert_generated_helper_for :products 
    174     assert_generated_stylesheet :scaffold 
    175     assert_generated_views_for :products, "index.html.erb", "new.html.erb", "edit.html.erb", "show.html.erb" 
    176     assert_generated_migration :create_products do |t| 
    177       assert_generated_column t, :name, :string 
    178       assert_generated_column t, :supplier_id, :integer 
    179       assert_generated_column t, :created_at, :timestamp 
    180     end 
    181  
    182     assert_added_route_for :products 
    183188  end 
    184  
    185 end 
     189end 
  • railties/test/generators/generator_test_helper.rb

    old new  
     1require 'initializer' 
     2 
     3# Mocks out the configuration 
     4module Rails 
     5  def self.configuration 
     6    Rails::Configuration.new 
     7  end 
     8end 
     9 
     10require 'rails_generator' 
     11 
     12 
    113module GeneratorTestHelper 
    214  # Instantiates the Generator 
    315  def build_generator(name,params) 
     
    5163  # asserts that the given functional test was generated. 
    5264  # It takes a name or symbol without the <tt>_controller_test</tt> part and an optional super class. 
    5365  # the contents of the class source file is passed to a block. 
    54   def assert_generated_functional_test_for(name,parent="Test::Unit::TestCase") 
     66  def assert_generated_functional_test_for(name,parent="ActionController::TestCase") 
    5567    assert_generated_class "test/functional/#{name.to_s.underscore}_controller_test",parent do |body| 
    5668      yield body if block_given? 
    5769    end 
     
    6072  # asserts that the given unit test was generated. 
    6173  # It takes a name or symbol without the <tt>_test</tt> part and an optional super class. 
    6274  # the contents of the class source file is passed to a block. 
    63   def assert_generated_unit_test_for(name,parent="Test::Unit::TestCase") 
     75  def assert_generated_unit_test_for(name,parent="ActiveSupport::TestCase") 
    6476    assert_generated_class "test/unit/#{name.to_s.underscore}_test",parent do |body| 
    6577      yield body if block_given? 
    6678    end 
     
    7789 
    7890  # asserts that the given file exists 
    7991  def assert_file_exists(path) 
    80     assert File.exist?("#{RAILS_ROOT}/#{path}"),"The file '#{path}' should exist" 
     92    assert File.exist?("#{RAILS_ROOT}/#{path}"),"The file '#{RAILS_ROOT}/#{path}' should exist" 
    8193  end 
    8294 
    8395  # asserts that the given class source file was generated. 
     
    128140  # the parsed yaml tree is passed to a block. 
    129141  def assert_generated_fixtures_for(name) 
    130142    assert_generated_yaml "test/fixtures/#{name.to_s.underscore}" do |yaml| 
    131       assert_generated_timestamps(yaml) 
    132143      yield yaml if block_given? 
    133144    end 
    134145  end 
     
    144155    end 
    145156  end 
    146157 
    147   # asserts that the given migration file was generated. 
    148   # It takes the name of the migration as a parameter. 
    149   # The migration body is passed to a block. 
    150158  def assert_generated_migration(name,parent="ActiveRecord::Migration") 
    151     assert_generated_class "db/migrate/001_#{name.to_s.underscore}",parent do |body| 
    152       assert body=~/timestamps/, "should have timestamps defined" 
    153       yield body if block_given? 
     159      file = 
     160   Dir.glob("#{RAILS_ROOT}/db/migrate/*_#{name.to_s.underscore}.rb").first 
     161      file = file.match(/db\/migrate\/[0-9]+_#{name.to_s.underscore}/).to_s 
     162      assert_generated_class file,parent do |body| 
     163        assert body=~/timestamps/, "should have timestamps defined" 
     164        yield body if block_given? 
     165      end 
    154166    end 
    155   end 
    156  
     167     
    157168  # Asserts that the given migration file was not generated. 
    158169  # It takes the name of the migration as a parameter. 
    159170  def assert_skipped_migration(name) 
     
    182193  def assert_generated_column(body,name,type) 
    183194      assert body=~/t\.#{type.to_s} :#{name.to_s}/, "should have column #{name.to_s} defined" 
    184195  end 
    185  
    186   private 
    187     # asserts that the default timestamps are created in the fixture 
    188     def assert_generated_timestamps(yaml) 
    189       yaml.values.each do |v| 
    190         ["created_at", "updated_at"].each do |field| 
    191           assert v.keys.include?(field), "should have #{field} field by default" 
    192         end 
    193       end 
    194     end 
    195196end 
  • railties/test/generators/rails_resource_generator_test.rb

    old new  
    4949Dir.mkdir(RAILS_ROOT) unless File.exist?(RAILS_ROOT) 
    5050 
    5151$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib" 
    52 require 'rails_generator' 
    5352require 'generators/generator_test_helper' 
    5453 
    5554class RailsResourceGeneratorTest < Test::Unit::TestCase 
     
    8079  end 
    8180 
    8281  def test_resource_generates_resources 
    83     run_generator('scaffold', %w(Product)) 
     82    run_generator('resource', %w(Product name:string)) 
    8483 
    8584    assert_generated_controller_for :products 
    8685    assert_generated_model_for :product 
     
    9291  end 
    9392 
    9493  def test_resource_skip_migration_skips_migration 
    95     run_generator('resource', %w(Product --skip-migration)) 
     94    run_generator('resource', %w(Product name:string --skip-migration)) 
    9695 
    9796    assert_generated_controller_for :products 
    9897    assert_generated_model_for :product 
  • railties/lib/rails_generator/commands.rb

    old new  
    301301          else 
    302302            logger.create relative_path 
    303303                  unless options[:pretend] 
    304                    FileUtils.mkdir_p(path) 
     304              FileUtils.mkdir_p(path) 
    305305                    # git doesn't require adding the paths, adding the files later will 
    306306                    # automatically do a path add. 
    307307