未验证 提交 a4f16d93 编写于 作者: R Rafael França 提交者: GitHub

Merge pull request #33322 from utilum/test_value_instead_of_method_call

Reduce mocking by testing value instead of method call
......@@ -123,6 +123,13 @@ def setup
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 @@ def test_ignores_remote_databases
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 @@ def setup
@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 @@ def test_ignores_remote_databases
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
......
......@@ -66,11 +66,8 @@ def test_establishes_connection_to_new_database
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
......
......@@ -61,11 +61,8 @@ def test_db_create_establishes_a_connection
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册