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 def lock_thread=(lock_thread); end
end.new end.new
connection.expects(:begin_transaction).with(joinable: false) assert_called_with(connection, :begin_transaction, [joinable: false]) do
fire_connection_notification(connection)
fire_connection_notification(connection) end
end end
def test_notification_established_transactions_are_rolled_back def test_notification_established_transactions_are_rolled_back
@ -891,7 +891,7 @@ class TransactionalFixturesOnConnectionNotification < ActiveRecord::TestCase
private private
def fire_connection_notification(connection) 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 message_bus = ActiveSupport::Notifications.instrumenter
payload = { payload = {
spec_name: "book", spec_name: "book",

View file

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

View file

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

View file

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

View file

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