diff options
Diffstat (limited to 'railties/test')
-rw-r--r-- | railties/test/application/runner_test.rb | 2 | ||||
-rw-r--r-- | railties/test/application/test_test.rb | 34 | ||||
-rw-r--r-- | railties/test/commands/console_test.rb | 36 | ||||
-rw-r--r-- | railties/test/commands/dbconsole_test.rb | 77 | ||||
-rw-r--r-- | railties/test/commands/server_test.rb | 3 | ||||
-rw-r--r-- | railties/test/generators/plugin_test_runner_test.rb | 6 |
6 files changed, 127 insertions, 31 deletions
diff --git a/railties/test/application/runner_test.rb b/railties/test/application/runner_test.rb index 4dc766dfda..77e7a2cca5 100644 --- a/railties/test/application/runner_test.rb +++ b/railties/test/application/runner_test.rb @@ -82,7 +82,7 @@ module ApplicationTests def test_runner_detects_bad_script_name output = Dir.chdir(app_path) { `bin/rails runner "iuiqwiourowe" 2>&1` } assert_not $?.success? - assert_match "undefined local variable or method `iuiqwiourowe' for main:Object", output + assert_match "undefined local variable or method `iuiqwiourowe' for", output end def test_environment_with_rails_env diff --git a/railties/test/application/test_test.rb b/railties/test/application/test_test.rb index 838adbbda9..32d2a6857c 100644 --- a/railties/test/application/test_test.rb +++ b/railties/test/application/test_test.rb @@ -12,7 +12,7 @@ module ApplicationTests teardown_app end - test "truth" do + test "simple successful test" do app_file "test/unit/foo_test.rb", <<-RUBY require 'test_helper' @@ -26,6 +26,38 @@ module ApplicationTests assert_successful_test_run "unit/foo_test.rb" end + test "after_run" do + app_file "test/unit/foo_test.rb", <<-RUBY + require 'test_helper' + + Minitest.after_run { puts "WORLD" } + Minitest.after_run { puts "HELLO" } + + class FooTest < ActiveSupport::TestCase + def test_truth + assert true + end + end + RUBY + + result = assert_successful_test_run "unit/foo_test.rb" + assert_equal ["HELLO", "WORLD"], result.scan(/HELLO|WORLD/) # only once and in correct order + end + + test "simple failed test" do + app_file "test/unit/foo_test.rb", <<-RUBY + require 'test_helper' + + class FooTest < ActiveSupport::TestCase + def test_truth + assert false + end + end + RUBY + + assert_unsuccessful_run "unit/foo_test.rb", "Failed assertion" + end + test "integration test" do controller "posts", <<-RUBY class PostsController < ActionController::Base diff --git a/railties/test/commands/console_test.rb b/railties/test/commands/console_test.rb index 96ac7c0661..4fc082e4ca 100644 --- a/railties/test/commands/console_test.rb +++ b/railties/test/commands/console_test.rb @@ -1,6 +1,7 @@ require "abstract_unit" require "env_helpers" -require "rails/commands/console" +require "rails/command" +require "rails/commands/console/console_command" class Rails::ConsoleTest < ActiveSupport::TestCase include EnvHelpers @@ -102,13 +103,21 @@ class Rails::ConsoleTest < ActiveSupport::TestCase end def test_rails_env_is_dev_when_argument_is_dev_and_dev_env_is_present - stubbed_console = Class.new(Rails::Console) do - def available_environments + Rails::Command::ConsoleCommand.class_eval do + alias_method :old_environments, :available_environments + + define_method :available_environments do ["dev"] end end - options = stubbed_console.parse_arguments(["dev"]) - assert_match("dev", options[:environment]) + + assert_match("dev", parse_arguments(["dev"])[:environment]) + ensure + Rails::Command::ConsoleCommand.class_eval do + undef_method :available_environments + alias_method :available_environments, :old_environments + undef_method :old_environments + end end attr_reader :output @@ -148,6 +157,21 @@ class Rails::ConsoleTest < ActiveSupport::TestCase end def parse_arguments(args) - Rails::Console.parse_arguments(args) + Rails::Command::ConsoleCommand.class_eval do + alias_method :old_perform, :perform + define_method(:perform) do + extract_environment_option_from_argument + + options + end + end + + Rails::Command.invoke(:console, args) + ensure + Rails::Command::ConsoleCommand.class_eval do + undef_method :perform + alias_method :perform, :old_perform + undef_method :old_perform + end end end diff --git a/railties/test/commands/dbconsole_test.rb b/railties/test/commands/dbconsole_test.rb index 286e7c16c1..2ddb269eae 100644 --- a/railties/test/commands/dbconsole_test.rb +++ b/railties/test/commands/dbconsole_test.rb @@ -1,6 +1,7 @@ require "abstract_unit" require "minitest/mock" -require "rails/commands/dbconsole" +require "rails/command" +require "rails/commands/dbconsole/dbconsole_command" class Rails::DBConsoleTest < ActiveSupport::TestCase def setup @@ -97,16 +98,14 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase end def test_rails_env_is_development_when_argument_is_dev - Rails::DBConsole.stub(:available_environments, ["development", "test"]) do - options = Rails::DBConsole.send(:parse_arguments, ["dev"]) - assert_match("development", options[:environment]) + stub_available_environments([ "development", "test" ]) do + assert_match("development", parse_arguments([ "dev" ])[:environment]) end end def test_rails_env_is_dev_when_argument_is_dev_and_dev_env_is_present - Rails::DBConsole.stub(:available_environments, ["dev"]) do - options = Rails::DBConsole.send(:parse_arguments, ["dev"]) - assert_match("dev", options[:environment]) + stub_available_environments([ "dev" ]) do + assert_match("dev", parse_arguments([ "dev" ])[:environment]) end end @@ -203,20 +202,16 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase def test_print_help_short stdout = capture(:stdout) do - start({}, ["-h"]) + Rails::Command.invoke(:dbconsole, ["-h"]) end - assert aborted - assert_equal "", output - assert_match(/Usage:.*dbconsole/, stdout) + assert_match(/bin\/rails dbconsole \[environment\]/, stdout) end def test_print_help_long stdout = capture(:stdout) do - start({}, ["--help"]) + Rails::Command.invoke(:dbconsole, ["--help"]) end - assert aborted - assert_equal "", output - assert_match(/Usage:.*dbconsole/, stdout) + assert_match(/bin\/rails dbconsole \[environment\]/, stdout) end attr_reader :aborted, :output @@ -230,21 +225,22 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase end end - def dbconsole - @dbconsole ||= Class.new(Rails::DBConsole) do + def make_dbconsole + Class.new(Rails::DBConsole) do attr_reader :find_cmd_and_exec_args def find_cmd_and_exec(*args) @find_cmd_and_exec_args = args end - end.new(nil) + end end + attr_reader :dbconsole + def start(config = {}, argv = []) - dbconsole.stub(:config, config.stringify_keys) do - dbconsole.stub(:arguments, argv) do - capture_abort { dbconsole.start } - end + @dbconsole = make_dbconsole.new(parse_arguments(argv)) + @dbconsole.stub(:config, config.stringify_keys) do + capture_abort { @dbconsole.start } end end @@ -258,4 +254,41 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase end end end + + def stub_available_environments(environments) + Rails::Command::DbconsoleCommand.class_eval do + alias_method :old_environments, :available_environments + + define_method :available_environments do + environments + end + end + + yield + ensure + Rails::Command::DbconsoleCommand.class_eval do + undef_method :available_environments + alias_method :available_environments, :old_environments + undef_method :old_environments + end + end + + def parse_arguments(args) + Rails::Command::DbconsoleCommand.class_eval do + alias_method :old_perform, :perform + define_method(:perform) do + extract_environment_option_from_argument + + options + end + end + + Rails::Command.invoke(:dbconsole, args) + ensure + Rails::Command::DbconsoleCommand.class_eval do + undef_method :perform + alias_method :perform, :old_perform + undef_method :old_perform + end + end end diff --git a/railties/test/commands/server_test.rb b/railties/test/commands/server_test.rb index c1ec73d95c..391886bf33 100644 --- a/railties/test/commands/server_test.rb +++ b/railties/test/commands/server_test.rb @@ -1,6 +1,7 @@ require "abstract_unit" require "env_helpers" -require "rails/commands/server" +require "rails/command" +require "rails/commands/server/server_command" class Rails::ServerTest < ActiveSupport::TestCase include EnvHelpers diff --git a/railties/test/generators/plugin_test_runner_test.rb b/railties/test/generators/plugin_test_runner_test.rb index 04b4b10254..7a10a2afa9 100644 --- a/railties/test/generators/plugin_test_runner_test.rb +++ b/railties/test/generators/plugin_test_runner_test.rb @@ -86,6 +86,12 @@ class PluginTestRunnerTest < ActiveSupport::TestCase assert_match(%r{cannot load such file.+test/not_exists\.rb}, error) end + def test_executed_only_once + create_test_file "foo" + result = run_test_command("test/foo_test.rb") + assert_equal 1, result.scan(/1 runs, 1 assertions, 0 failures/).length + end + private def plugin_path "#{@destination_root}/bukkits" |