aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2017-12-08 13:23:31 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2017-12-08 13:23:31 -0800
commita58543dbb1ea52f3cb0c98d054ffd7bc7a373765 (patch)
tree2f68bf52a4f9e14ee8142e3851b00e61af117820
parentda225c0db640aec1df0920e86c1eb4ca35d82073 (diff)
downloadrails-a58543dbb1ea52f3cb0c98d054ffd7bc7a373765.tar.gz
rails-a58543dbb1ea52f3cb0c98d054ffd7bc7a373765.tar.bz2
rails-a58543dbb1ea52f3cb0c98d054ffd7bc7a373765.zip
Add failing test for wrong database connection
When tests are run with just `ruby`, the RAILS_ENV is set to `development` too early, and we connect to the development database rather than the test database.
-rw-r--r--railties/test/application/test_runner_test.rb17
1 files changed, 17 insertions, 0 deletions
diff --git a/railties/test/application/test_runner_test.rb b/railties/test/application/test_runner_test.rb
index 27983c3b55..17333b6ac9 100644
--- a/railties/test/application/test_runner_test.rb
+++ b/railties/test/application/test_runner_test.rb
@@ -570,6 +570,10 @@ module ApplicationTests
end
def test_running_with_ruby_gets_test_env_by_default
+ # Subshells inherit `ENV`, so we need to ensure `RAILS_ENV` is set to
+ # nil before we run the tests in the test app.
+ re, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], nil
+
file = create_test_for_env("test")
results = Dir.chdir(app_path) {
`ruby -Ilib:test #{file}`.each_line.map { |line| JSON.parse line }
@@ -577,9 +581,16 @@ module ApplicationTests
assert_equal 1, results.length
failures = results.first["failures"]
flunk(failures.first) unless failures.empty?
+
+ ensure
+ ENV["RAILS_ENV"] = re
end
def test_running_with_ruby_can_set_env_via_cmdline
+ # Subshells inherit `ENV`, so we need to ensure `RAILS_ENV` is set to
+ # nil before we run the tests in the test app.
+ re, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], nil
+
file = create_test_for_env("development")
results = Dir.chdir(app_path) {
`ruby -Ilib:test #{file} -e development`.each_line.map { |line| JSON.parse line }
@@ -587,6 +598,9 @@ module ApplicationTests
assert_equal 1, results.length
failures = results.first["failures"]
flunk(failures.first) unless failures.empty?
+
+ ensure
+ ENV["RAILS_ENV"] = re
end
def test_rake_passes_multiple_TESTOPTS_to_minitest
@@ -777,6 +791,9 @@ module ApplicationTests
class EnvironmentTest < ActiveSupport::TestCase
def test_environment
+ test_db = ActiveRecord::Base.configurations[#{env.dump}]["database"]
+ db_file = ActiveRecord::Base.connection_config[:database]
+ assert_match(test_db, db_file)
assert_equal #{env.dump}, ENV["RAILS_ENV"]
end
end