mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Make sure that rails test
load test in test env
This commit is contained in:
parent
b51673fbd9
commit
3ed41e579e
4 changed files with 55 additions and 7 deletions
|
@ -84,10 +84,9 @@ when 'test'
|
|||
$LOAD_PATH.unshift("./test")
|
||||
require 'rails/commands/test_runner'
|
||||
options = Rails::TestRunner.parse_arguments(ARGV)
|
||||
ENV['RAILS_ENV'] ||= options[:environment] || 'test'
|
||||
|
||||
require APP_PATH
|
||||
Rails.application.require_environment!
|
||||
Rails.application.load_tasks
|
||||
Rails::TestRunner.start(ARGV, options)
|
||||
|
||||
when 'dbconsole'
|
||||
|
|
|
@ -56,6 +56,10 @@ module Rails
|
|||
exit
|
||||
end
|
||||
|
||||
opts.on '-e', '--environment NAME', String, 'Specifies the environment to run this test under' do |e|
|
||||
options[:environment] = e
|
||||
end
|
||||
|
||||
opts.on '-f', '--fixtures', 'Load fixtures in test/fixtures/ before running the tests' do
|
||||
options[:fixtures] = true
|
||||
end
|
||||
|
@ -68,8 +72,8 @@ module Rails
|
|||
options[:verbose] = true
|
||||
end
|
||||
|
||||
opts.on '-n', '--name PATTERN', "Filter test names on pattern (e.g. /foo/)" do |a|
|
||||
options[:filter] = a
|
||||
opts.on '-n', '--name PATTERN', "Filter test names on pattern (e.g. /foo/)" do |n|
|
||||
options[:filter] = n
|
||||
end
|
||||
|
||||
opts.separator ""
|
||||
|
@ -94,7 +98,9 @@ module Rails
|
|||
# Creates a new +TestRunner+ object with a list of test file paths.
|
||||
def initialize(files, options)
|
||||
@files = files
|
||||
Rake::Task['test:prepare'].invoke
|
||||
|
||||
Rails.application.load_tasks
|
||||
Rake::Task['db:test:load'].invoke
|
||||
|
||||
if options.delete(:fixtures)
|
||||
if defined?(ActiveRecord::Base)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
ENV["RAILS_ENV"] = "test"
|
||||
ENV["RAILS_ENV"] ||= "test"
|
||||
require File.expand_path('../../config/environment', __FILE__)
|
||||
require 'rails/test_help'
|
||||
|
||||
|
|
|
@ -191,6 +191,39 @@ module ApplicationTests
|
|||
end
|
||||
end
|
||||
|
||||
def test_run_different_environment_using_env_var
|
||||
app_file 'test/unit/env_test.rb', <<-RUBY
|
||||
require 'test_helper'
|
||||
|
||||
class EnvTest < ActiveSupport::TestCase
|
||||
def test_env
|
||||
puts Rails.env
|
||||
end
|
||||
end
|
||||
RUBY
|
||||
|
||||
assert_match /development/, Dir.chdir(app_path) { `RAILS_ENV=development bundle exec rails test test/unit/env_test.rb` }
|
||||
end
|
||||
|
||||
def test_run_different_environment_using_e_tag
|
||||
app_file 'test/unit/env_test.rb', <<-RUBY
|
||||
require 'test_helper'
|
||||
|
||||
class EnvTest < ActiveSupport::TestCase
|
||||
def test_env
|
||||
puts Rails.env
|
||||
end
|
||||
end
|
||||
RUBY
|
||||
|
||||
assert_match /development/, run_test_command('-e development test/unit/env_test.rb')
|
||||
end
|
||||
|
||||
def test_generated_scaffold_works_with_rails_test
|
||||
create_scaffold
|
||||
assert_match /0 failures, 0 errors, 0 skips/, run_test_command('')
|
||||
end
|
||||
|
||||
private
|
||||
def run_test_command(arguments = 'test/unit/test_test.rb')
|
||||
Dir.chdir(app_path) { `bundle exec rails test #{arguments}` }
|
||||
|
@ -211,7 +244,7 @@ module ApplicationTests
|
|||
name: Tsubasa Hanekawa
|
||||
YAML
|
||||
|
||||
Dir.chdir(app_path) { `bundle exec rake db:migrate` }
|
||||
run_migration
|
||||
end
|
||||
|
||||
def create_fixture_test(path = :unit, name = 'test')
|
||||
|
@ -251,5 +284,15 @@ module ApplicationTests
|
|||
end
|
||||
RUBY
|
||||
end
|
||||
|
||||
def create_scaffold
|
||||
script 'generate scaffold user name:string'
|
||||
Dir.chdir(app_path) { File.exist?('app/models/user.rb') }
|
||||
run_migration
|
||||
end
|
||||
|
||||
def run_migration
|
||||
Dir.chdir(app_path) { `bundle exec rake db:migrate` }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue