diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 6bf9b22a2d282e5e7c021bf75ae0de538c852d7a..4145938063354b6b3f9ece9e257edfaaf04d8aa3 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -19,6 +19,14 @@ *Terence Lee* +* Rails now generate a `test/test_helper.rb` file with `fixtures :all` commented out by default, + since we don't want to force loading all fixtures for user when a single test is run. However, + fixtures are still going to be loaded automatically for test suites. + + To force all fixtures to be create in your database, use `rails test -f` to run your test. + + *Prem Sichanugrist* + * Add `rails test` command to run the test suite To run the whole test suite: diff --git a/railties/lib/rails/commands/test_runner.rb b/railties/lib/rails/commands/test_runner.rb index 33c32bb94f375391956202d2ead478a544131c85..ae901f05f82c175584999bd224688f66a392eb34 100644 --- a/railties/lib/rails/commands/test_runner.rb +++ b/railties/lib/rails/commands/test_runner.rb @@ -9,7 +9,10 @@ class << self # of file to a new +TestRunner+ object, then invoke the evaluation. If # the argument is not a test suite name, it will be treated as a file # name and passed to the +TestRunner+ instance right away. - def start(files, options) + def start(files, options = {}) + original_fixtures_options = options.delete(:fixtures) + options[:fixtures] = true + case files.first when nil new(Dir['test/**/*_test.rb'], options).run @@ -28,6 +31,7 @@ def start(files, options) when 'integration' new(Dir['test/integration/**/*_test.rb'], options).run else + options[:fixtures] = original_fixtures_options new(files, options).run end end @@ -52,6 +56,10 @@ def parse_arguments(arguments) exit end + opts.on '-f', '--fixtures', 'Load fixtures in test/fixtures/ before running the tests' do + options[:fixtures] = true + end + opts.on '-s', '--seed SEED', Integer, "Sets random seed" do |m| options[:seed] = m.to_i end @@ -87,6 +95,15 @@ def parse_arguments(arguments) def initialize(files, options) @files = files Rake::Task['test:prepare'].invoke + + if options.delete(:fixtures) + if defined?(ActiveRecord::Base) + ActiveSupport::TestCase.send :include, ActiveRecord::TestFixtures + ActiveSupport::TestCase.fixture_path = "#{Rails.root}/test/fixtures/" + ActiveSupport::TestCase.fixtures :all + end + end + MiniTest::Unit.runner.options = options MiniTest::Unit.output = SilentUntilSyncStream.new(MiniTest::Unit.output) end diff --git a/railties/lib/rails/generators/rails/app/templates/test/test_helper.rb b/railties/lib/rails/generators/rails/app/templates/test/test_helper.rb index 9afda2d0df1feb8d769a18be19d9ecf07cd523dd..754e99e09fb808cf02a5fcbac0a62cc84e55c799 100644 --- a/railties/lib/rails/generators/rails/app/templates/test/test_helper.rb +++ b/railties/lib/rails/generators/rails/app/templates/test/test_helper.rb @@ -6,11 +6,12 @@ class ActiveSupport::TestCase <% unless options[:skip_active_record] -%> ActiveRecord::Migration.check_pending! - # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. + # Uncomment the `fixtures` line below to setup all fixtures in test/fixtures/*.yml for all tests + # in alphabetical order. # # Note: You'll currently still have to declare fixtures explicitly in integration tests # -- they do not yet inherit this setting - fixtures :all + # fixtures :all <% end -%> # Add more helper methods to be used by all tests here... diff --git a/railties/test/application/test_runner_test.rb b/railties/test/application/test_runner_test.rb index 249bfd1d5ddad059a6e95ab9bb2d4f7e6e27b6f6..71e2b403afea4ca6bf3676d8ffd21a67b2b61e96 100644 --- a/railties/test/application/test_runner_test.rb +++ b/railties/test/application/test_runner_test.rb @@ -1,4 +1,5 @@ require 'isolation/abstract_unit' +require 'active_support/core_ext/string/strip' module ApplicationTests class TestRunnerTest < ActiveSupport::TestCase @@ -170,11 +171,61 @@ def test_sanae end end + def test_not_load_fixtures_when_running_single_test + create_model_with_fixture + create_fixture_test :models, 'user' + assert_match /0 users/, run_test_command('test/models/user_test.rb') + assert_match /3 users/, run_test_command('test/models/user_test.rb -f') + end + + def test_load_fixtures_when_running_test_suites + create_model_with_fixture + types = [:models, :helpers, [:units, :unit], :controllers, :mailers, + [:functionals, :functional], :integration] + + types.each do |type, directory| + directory ||= type + create_fixture_test directory + assert_match /3 users/, run_test_command(type) + Dir.chdir(app_path) { FileUtils.rm_f "test/#{directory}" } + end + end + private def run_test_command(arguments = 'test/unit/test_test.rb') Dir.chdir(app_path) { `bundle exec rails test #{arguments}` } end + def create_model_with_fixture + script 'generate model user name:string' + + app_file 'test/fixtures/users.yml', <<-YAML.strip_heredoc + vampire: + id: 1 + name: Koyomi Araragi + crab: + id: 2 + name: Senjougahara Hitagi + cat: + id: 3 + name: Tsubasa Hanekawa + YAML + + Dir.chdir(app_path) { `bundle exec rake db:migrate` } + end + + def create_fixture_test(path = :unit, name = 'test') + app_file "test/#{path}/#{name}_test.rb", <<-RUBY + require 'test_helper' + + class #{name.camelize}Test < ActiveSupport::TestCase + def test_fixture + puts "\#{User.count} users (\#{__FILE__})" + end + end + RUBY + end + def create_schema app_file 'db/schema.rb', '' end