From 331cca17587f2ef23d69a5288a93d6444da2c980 Mon Sep 17 00:00:00 2001 From: utilum Date: Wed, 20 Jun 2018 02:32:12 +0200 Subject: Reduce mocking by testing value instead of method call Step 2 in #33162 --- .../test/cases/tasks/database_tasks_test.rb | 32 +++++++++++++++------- .../test/cases/tasks/postgresql_rake_test.rb | 5 +--- activerecord/test/cases/tasks/sqlite_rake_test.rb | 5 +--- 3 files changed, 24 insertions(+), 18 deletions(-) (limited to 'activerecord/test/cases') diff --git a/activerecord/test/cases/tasks/database_tasks_test.rb b/activerecord/test/cases/tasks/database_tasks_test.rb index a6f684a137..159590a810 100644 --- a/activerecord/test/cases/tasks/database_tasks_test.rb +++ b/activerecord/test/cases/tasks/database_tasks_test.rb @@ -123,6 +123,13 @@ module ActiveRecord ActiveRecord::Base.stubs(:configurations).returns(@configurations) # To refrain from connecting to a newly created empty DB in sqlite3_mem tests ActiveRecord::Base.connection_handler.stubs(:establish_connection) + + $stdout, @original_stdout = StringIO.new, $stdout + $stderr, @original_stderr = StringIO.new, $stderr + end + + def teardown + $stdout, $stderr = @original_stdout, @original_stderr end def test_ignores_configurations_without_databases @@ -145,9 +152,10 @@ module ActiveRecord def test_warning_for_remote_databases @configurations["development"].merge!("host" => "my.server.tld") - assert_called_with($stderr, :puts, ["This task only modifies local databases. my-db is on a remote host."]) do - ActiveRecord::Tasks::DatabaseTasks.create_all - end + ActiveRecord::Tasks::DatabaseTasks.create_all + + assert_match "This task only modifies local databases. my-db is on a remote host.", + $stderr.string end def test_creates_configurations_with_local_ip @@ -345,6 +353,13 @@ module ActiveRecord @configurations = { development: { "database" => "my-db" } } ActiveRecord::Base.stubs(:configurations).returns(@configurations) + + $stdout, @original_stdout = StringIO.new, $stdout + $stderr, @original_stderr = StringIO.new, $stderr + end + + def teardown + $stdout, $stderr = @original_stdout, @original_stderr end def test_ignores_configurations_without_databases @@ -367,13 +382,10 @@ module ActiveRecord def test_warning_for_remote_databases @configurations[:development].merge!("host" => "my.server.tld") - assert_called_with( - $stderr, - :puts, - ["This task only modifies local databases. my-db is on a remote host."], - ) do - ActiveRecord::Tasks::DatabaseTasks.drop_all - end + ActiveRecord::Tasks::DatabaseTasks.drop_all + + assert_match "This task only modifies local databases. my-db is on a remote host.", + $stderr.string end def test_drops_configurations_with_local_ip diff --git a/activerecord/test/cases/tasks/postgresql_rake_test.rb b/activerecord/test/cases/tasks/postgresql_rake_test.rb index bdce1cbd20..d2474ba786 100644 --- a/activerecord/test/cases/tasks/postgresql_rake_test.rb +++ b/activerecord/test/cases/tasks/postgresql_rake_test.rb @@ -66,11 +66,8 @@ if current_adapter?(:PostgreSQLAdapter) def test_db_create_with_error_prints_message ActiveRecord::Base.stubs(:establish_connection).raises(Exception) - $stderr.stubs(:puts) - $stderr.expects(:puts). - with("Couldn't create database for #{@configuration.inspect}") - assert_raises(Exception) { ActiveRecord::Tasks::DatabaseTasks.create @configuration } + assert_match "Couldn't create database for #{@configuration.inspect}", $stderr.string end def test_when_database_created_successfully_outputs_info_to_stdout diff --git a/activerecord/test/cases/tasks/sqlite_rake_test.rb b/activerecord/test/cases/tasks/sqlite_rake_test.rb index 7b4cb51c56..f1edae43f4 100644 --- a/activerecord/test/cases/tasks/sqlite_rake_test.rb +++ b/activerecord/test/cases/tasks/sqlite_rake_test.rb @@ -61,11 +61,8 @@ if current_adapter?(:SQLite3Adapter) def test_db_create_with_error_prints_message ActiveRecord::Base.stubs(:establish_connection).raises(Exception) - $stderr.stubs(:puts).returns(true) - $stderr.expects(:puts). - with("Couldn't create database for #{@configuration.inspect}") - assert_raises(Exception) { ActiveRecord::Tasks::DatabaseTasks.create @configuration, "/rails/root" } + assert_match "Couldn't create database for #{@configuration.inspect}", $stderr.string end end -- cgit v1.2.3