1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Use MethodCallAssertions instead of Mocha#expects

Many calls to `Mocha#expects` preceded the introduction of
`ActiveSupport::Testing::MethodCallAssertions` in 53f64c0fb,
and many are simple to replace with `MethodCallAssertions`.

This patch makes all these simple replacements.

Step 5 in #33162
This commit is contained in:
utilum 2018-07-19 01:25:25 +03:00
parent 08813dd62a
commit d0743d0263
5 changed files with 154 additions and 98 deletions

View file

@ -861,9 +861,9 @@ class TransactionalFixturesOnConnectionNotification < ActiveRecord::TestCase
def lock_thread=(lock_thread); end
end.new
connection.expects(:begin_transaction).with(joinable: false)
fire_connection_notification(connection)
assert_called_with(connection, :begin_transaction, [joinable: false]) do
fire_connection_notification(connection)
end
end
def test_notification_established_transactions_are_rolled_back
@ -891,7 +891,7 @@ class TransactionalFixturesOnConnectionNotification < ActiveRecord::TestCase
private
def fire_connection_notification(connection)
ActiveRecord::Base.connection_handler.stub(:retrieve_connection, connection) do
assert_called_with(ActiveRecord::Base.connection_handler, :retrieve_connection, ["book"], returns: connection) do
message_bus = ActiveSupport::Notifications.instrumenter
payload = {
spec_name: "book",

View file

@ -15,6 +15,7 @@ module ActiveRecord
def charset; end
def collation; end
def structure_dump(*); end
def structure_load(*); end
end.new
)
@ -119,8 +120,9 @@ module ActiveRecord
ADAPTERS_TASKS.each do |k, v|
define_method("test_#{k}_create") do
with_stubbed_new do
eval("@#{v}").expects(:create)
ActiveRecord::Tasks::DatabaseTasks.create "adapter" => k
assert_called(eval("@#{v}"), :create) do
ActiveRecord::Tasks::DatabaseTasks.create "adapter" => k
end
end
end
end
@ -407,11 +409,15 @@ module ActiveRecord
def test_establishes_connection_for_the_given_environments_config
ActiveRecord::Tasks::DatabaseTasks.stub(:create, nil) do
ActiveRecord::Base.expects(:establish_connection).with(:development)
ActiveRecord::Tasks::DatabaseTasks.create_current(
ActiveSupport::StringInquirer.new("development")
)
assert_called_with(
ActiveRecord::Base,
:establish_connection,
[:development]
) do
ActiveRecord::Tasks::DatabaseTasks.create_current(
ActiveSupport::StringInquirer.new("development")
)
end
end
end
@ -432,8 +438,9 @@ module ActiveRecord
ADAPTERS_TASKS.each do |k, v|
define_method("test_#{k}_drop") do
with_stubbed_new do
eval("@#{v}").expects(:drop)
ActiveRecord::Tasks::DatabaseTasks.drop "adapter" => k
assert_called(eval("@#{v}"), :drop) do
ActiveRecord::Tasks::DatabaseTasks.drop "adapter" => k
end
end
end
end
@ -812,8 +819,9 @@ module ActiveRecord
ADAPTERS_TASKS.each do |k, v|
define_method("test_#{k}_purge") do
with_stubbed_new do
eval("@#{v}").expects(:purge)
ActiveRecord::Tasks::DatabaseTasks.purge "adapter" => k
assert_called(eval("@#{v}"), :purge) do
ActiveRecord::Tasks::DatabaseTasks.purge "adapter" => k
end
end
end
end
@ -827,11 +835,14 @@ module ActiveRecord
"production" => { "database" => "prod-db" }
}
ActiveRecord::Base.stub(:configurations, configurations) do
ActiveRecord::Tasks::DatabaseTasks.expects(:purge).
with("database" => "prod-db")
assert_called_with(ActiveRecord::Base, :establish_connection, [:production]) do
ActiveRecord::Tasks::DatabaseTasks.purge_current("production")
assert_called_with(
ActiveRecord::Tasks::DatabaseTasks,
:purge,
["database" => "prod-db"]
) do
assert_called_with(ActiveRecord::Base, :establish_connection, [:production]) do
ActiveRecord::Tasks::DatabaseTasks.purge_current("production")
end
end
end
end
@ -841,10 +852,13 @@ module ActiveRecord
def test_purge_all_local_configurations
configurations = { development: { "database" => "my-db" } }
ActiveRecord::Base.stub(:configurations, configurations) do
ActiveRecord::Tasks::DatabaseTasks.expects(:purge).
with("database" => "my-db")
ActiveRecord::Tasks::DatabaseTasks.purge_all
assert_called_with(
ActiveRecord::Tasks::DatabaseTasks,
:purge,
["database" => "my-db"]
) do
ActiveRecord::Tasks::DatabaseTasks.purge_all
end
end
end
end
@ -855,8 +869,9 @@ module ActiveRecord
ADAPTERS_TASKS.each do |k, v|
define_method("test_#{k}_charset") do
with_stubbed_new do
eval("@#{v}").expects(:charset)
ActiveRecord::Tasks::DatabaseTasks.charset "adapter" => k
assert_called(eval("@#{v}"), :charset) do
ActiveRecord::Tasks::DatabaseTasks.charset "adapter" => k
end
end
end
end
@ -868,8 +883,9 @@ module ActiveRecord
ADAPTERS_TASKS.each do |k, v|
define_method("test_#{k}_collation") do
with_stubbed_new do
eval("@#{v}").expects(:collation)
ActiveRecord::Tasks::DatabaseTasks.collation "adapter" => k
assert_called(eval("@#{v}"), :collation) do
ActiveRecord::Tasks::DatabaseTasks.collation "adapter" => k
end
end
end
end
@ -983,8 +999,12 @@ module ActiveRecord
ADAPTERS_TASKS.each do |k, v|
define_method("test_#{k}_structure_dump") do
with_stubbed_new do
eval("@#{v}").expects(:structure_dump).with("awesome-file.sql", nil)
ActiveRecord::Tasks::DatabaseTasks.structure_dump({ "adapter" => k }, "awesome-file.sql")
assert_called_with(
eval("@#{v}"), :structure_dump,
["awesome-file.sql", nil]
) do
ActiveRecord::Tasks::DatabaseTasks.structure_dump({ "adapter" => k }, "awesome-file.sql")
end
end
end
end
@ -996,8 +1016,13 @@ module ActiveRecord
ADAPTERS_TASKS.each do |k, v|
define_method("test_#{k}_structure_load") do
with_stubbed_new do
eval("@#{v}").expects(:structure_load).with("awesome-file.sql", nil)
ActiveRecord::Tasks::DatabaseTasks.structure_load({ "adapter" => k }, "awesome-file.sql")
assert_called_with(
eval("@#{v}"),
:structure_load,
["awesome-file.sql", nil]
) do
ActiveRecord::Tasks::DatabaseTasks.structure_load({ "adapter" => k }, "awesome-file.sql")
end
end
end
end

View file

@ -31,28 +31,29 @@ if current_adapter?(:Mysql2Adapter)
def test_creates_database_with_no_default_options
with_stubbed_connection_establish_connection do
@connection.expects(:create_database).
with("my-app-db", {})
ActiveRecord::Tasks::DatabaseTasks.create @configuration
assert_called_with(@connection, :create_database, ["my-app-db", {}]) do
ActiveRecord::Tasks::DatabaseTasks.create @configuration
end
end
end
def test_creates_database_with_given_encoding
with_stubbed_connection_establish_connection do
@connection.expects(:create_database).
with("my-app-db", charset: "latin1")
ActiveRecord::Tasks::DatabaseTasks.create @configuration.merge("encoding" => "latin1")
assert_called_with(@connection, :create_database, ["my-app-db", charset: "latin1"]) do
ActiveRecord::Tasks::DatabaseTasks.create @configuration.merge("encoding" => "latin1")
end
end
end
def test_creates_database_with_given_collation
with_stubbed_connection_establish_connection do
@connection.expects(:create_database).
with("my-app-db", collation: "latin1_swedish_ci")
ActiveRecord::Tasks::DatabaseTasks.create @configuration.merge("collation" => "latin1_swedish_ci")
assert_called_with(
@connection,
:create_database,
["my-app-db", collation: "latin1_swedish_ci"]
) do
ActiveRecord::Tasks::DatabaseTasks.create @configuration.merge("collation" => "latin1_swedish_ci")
end
end
end
@ -149,9 +150,9 @@ if current_adapter?(:Mysql2Adapter)
def test_drops_database
with_stubbed_connection_establish_connection do
@connection.expects(:drop_database).with("my-app-db")
ActiveRecord::Tasks::DatabaseTasks.drop @configuration
assert_called_with(@connection, :drop_database, ["my-app-db"]) do
ActiveRecord::Tasks::DatabaseTasks.drop @configuration
end
end
end
@ -193,20 +194,22 @@ if current_adapter?(:Mysql2Adapter)
def test_recreates_database_with_no_default_options
with_stubbed_connection_establish_connection do
@connection.expects(:recreate_database).
with("test-db", {})
ActiveRecord::Tasks::DatabaseTasks.purge @configuration
assert_called_with(@connection, :recreate_database, ["test-db", {}]) do
ActiveRecord::Tasks::DatabaseTasks.purge @configuration
end
end
end
def test_recreates_database_with_the_given_options
with_stubbed_connection_establish_connection do
@connection.expects(:recreate_database).
with("test-db", charset: "latin", collation: "latin1_swedish_ci")
ActiveRecord::Tasks::DatabaseTasks.purge @configuration.merge(
"encoding" => "latin", "collation" => "latin1_swedish_ci")
assert_called_with(
@connection,
:recreate_database,
["test-db", charset: "latin", collation: "latin1_swedish_ci"]
) do
ActiveRecord::Tasks::DatabaseTasks.purge @configuration.merge(
"encoding" => "latin", "collation" => "latin1_swedish_ci")
end
end
end
@ -232,8 +235,9 @@ if current_adapter?(:Mysql2Adapter)
def test_db_retrieves_charset
ActiveRecord::Base.stub(:connection, @connection) do
@connection.expects(:charset)
ActiveRecord::Tasks::DatabaseTasks.charset @configuration
assert_called(@connection, :charset) do
ActiveRecord::Tasks::DatabaseTasks.charset @configuration
end
end
end
end
@ -249,8 +253,9 @@ if current_adapter?(:Mysql2Adapter)
def test_db_retrieves_collation
ActiveRecord::Base.stub(:connection, @connection) do
@connection.expects(:collation)
ActiveRecord::Tasks::DatabaseTasks.collation @configuration
assert_called_with(@connection, :collation) do
ActiveRecord::Tasks::DatabaseTasks.collation @configuration
end
end
end
end

View file

@ -34,30 +34,46 @@ if current_adapter?(:PostgreSQLAdapter)
def test_creates_database_with_default_encoding
with_stubbed_connection_establish_connection do
@connection.expects(:create_database).
with("my-app-db", @configuration.merge("encoding" => "utf8"))
ActiveRecord::Tasks::DatabaseTasks.create @configuration
assert_called_with(
@connection,
:create_database,
["my-app-db", @configuration.merge("encoding" => "utf8")]
) do
ActiveRecord::Tasks::DatabaseTasks.create @configuration
end
end
end
def test_creates_database_with_given_encoding
with_stubbed_connection_establish_connection do
@connection.expects(:create_database).
with("my-app-db", @configuration.merge("encoding" => "latin"))
ActiveRecord::Tasks::DatabaseTasks.create @configuration.
merge("encoding" => "latin")
assert_called_with(
@connection,
:create_database,
["my-app-db", @configuration.merge("encoding" => "latin")]
) do
ActiveRecord::Tasks::DatabaseTasks.create @configuration.
merge("encoding" => "latin")
end
end
end
def test_creates_database_with_given_collation_and_ctype
with_stubbed_connection_establish_connection do
@connection.expects(:create_database).
with("my-app-db", @configuration.merge("encoding" => "utf8", "collation" => "ja_JP.UTF8", "ctype" => "ja_JP.UTF8"))
ActiveRecord::Tasks::DatabaseTasks.create @configuration.
merge("collation" => "ja_JP.UTF8", "ctype" => "ja_JP.UTF8")
assert_called_with(
@connection,
:create_database,
[
"my-app-db",
@configuration.merge(
"encoding" => "utf8",
"collation" => "ja_JP.UTF8",
"ctype" => "ja_JP.UTF8"
)
]
) do
ActiveRecord::Tasks::DatabaseTasks.create @configuration.
merge("collation" => "ja_JP.UTF8", "ctype" => "ja_JP.UTF8")
end
end
end
@ -139,9 +155,13 @@ if current_adapter?(:PostgreSQLAdapter)
def test_drops_database
with_stubbed_connection_establish_connection do
@connection.expects(:drop_database).with("my-app-db")
ActiveRecord::Tasks::DatabaseTasks.drop @configuration
assert_called_with(
@connection,
:drop_database,
["my-app-db"]
) do
ActiveRecord::Tasks::DatabaseTasks.drop @configuration
end
end
end
@ -179,9 +199,9 @@ if current_adapter?(:PostgreSQLAdapter)
def test_clears_active_connections
with_stubbed_connection do
ActiveRecord::Base.stub(:establish_connection, nil) do
ActiveRecord::Base.expects(:clear_active_connections!)
ActiveRecord::Tasks::DatabaseTasks.purge @configuration
assert_called(ActiveRecord::Base, :clear_active_connections!) do
ActiveRecord::Tasks::DatabaseTasks.purge @configuration
end
end
end
end
@ -202,9 +222,9 @@ if current_adapter?(:PostgreSQLAdapter)
def test_drops_database
with_stubbed_connection do
ActiveRecord::Base.stub(:establish_connection, nil) do
@connection.expects(:drop_database).with("my-app-db")
ActiveRecord::Tasks::DatabaseTasks.purge @configuration
assert_called_with(@connection, :drop_database, ["my-app-db"]) do
ActiveRecord::Tasks::DatabaseTasks.purge @configuration
end
end
end
end
@ -212,10 +232,13 @@ if current_adapter?(:PostgreSQLAdapter)
def test_creates_database
with_stubbed_connection do
ActiveRecord::Base.stub(:establish_connection, nil) do
@connection.expects(:create_database).
with("my-app-db", @configuration.merge("encoding" => "utf8"))
ActiveRecord::Tasks::DatabaseTasks.purge @configuration
assert_called_with(
@connection,
:create_database,
["my-app-db", @configuration.merge("encoding" => "utf8")]
) do
ActiveRecord::Tasks::DatabaseTasks.purge @configuration
end
end
end
end
@ -240,7 +263,10 @@ if current_adapter?(:PostgreSQLAdapter)
class PostgreSQLDBCharsetTest < ActiveRecord::TestCase
def setup
@connection = Class.new { def create_database(*); end }.new
@connection = Class.new do
def create_database(*); end
def encoding; end
end.new
@configuration = {
"adapter" => "postgresql",
"database" => "my-app-db"
@ -249,16 +275,16 @@ if current_adapter?(:PostgreSQLAdapter)
def test_db_retrieves_charset
ActiveRecord::Base.stub(:connection, @connection) do
@connection.expects(:encoding)
ActiveRecord::Tasks::DatabaseTasks.charset @configuration
assert_called(@connection, :encoding) do
ActiveRecord::Tasks::DatabaseTasks.charset @configuration
end
end
end
end
class PostgreSQLDBCollationTest < ActiveRecord::TestCase
def setup
@connection = Class.new { def create_database(*); end }.new
@connection = Class.new { def collation; end }.new
@configuration = {
"adapter" => "postgresql",
"database" => "my-app-db"
@ -267,9 +293,9 @@ if current_adapter?(:PostgreSQLAdapter)
def test_db_retrieves_collation
ActiveRecord::Base.stub(:connection, @connection) do
@connection.expects(:collation)
ActiveRecord::Tasks::DatabaseTasks.collation @configuration
assert_called(@connection, :collation) do
ActiveRecord::Tasks::DatabaseTasks.collation @configuration
end
end
end
end

View file

@ -88,9 +88,9 @@ if current_adapter?(:SQLite3Adapter)
end
def test_creates_path_from_database
Pathname.expects(:new).with(@database).returns(@path)
ActiveRecord::Tasks::DatabaseTasks.drop @configuration, "/rails/root"
assert_called_with(Pathname, :new, [@database], returns: @path) do
ActiveRecord::Tasks::DatabaseTasks.drop @configuration, "/rails/root"
end
end
def test_removes_file_with_absolute_path