Permalink
Browse files

Replace shallow mocks with Ruby classes

While preparing this I realised that some stubbed returns values
serve no purpose, so this patch drops those as well.

Step 3 in #33162
  • Loading branch information...
utilum committed Jul 10, 2018
1 parent d41ec81 commit f7bfb3db282f8333adb469b6d223b58523428d7d
@@ -833,29 +833,42 @@ class TransactionalFixturesOnConnectionNotification < ActiveRecord::TestCase
self.use_instantiated_fixtures = false
def test_transaction_created_on_connection_notification
connection = stub(transaction_open?: false)
connection = Class.new do
attr_accessor :pool
def transaction_open?; end
def begin_transaction(*args); end
def rollback_transaction(*args); end
end.new
connection.pool = Class.new do
def lock_thread=(lock_thread); false; end
end.new
connection.expects(:begin_transaction).with(joinable: false)
pool = connection.stubs(:pool).returns(ActiveRecord::ConnectionAdapters::ConnectionPool.new(ActiveRecord::Base.connection_pool.spec))
pool.stubs(:lock_thread=).with(false)
fire_connection_notification(connection)
end
def test_notification_established_transactions_are_rolled_back
# Mocha is not thread-safe so define our own stub to test
connection = Class.new do
attr_accessor :rollback_transaction_called
attr_accessor :pool
def transaction_open?; true; end
def begin_transaction(*args); end
def rollback_transaction(*args)
@rollback_transaction_called = true
end
end.new
connection.pool = Class.new do
def lock_thread=(lock_thread); false; end
end.new
fire_connection_notification(connection)
teardown_fixtures
assert(connection.rollback_transaction_called, "Expected <mock connection>#rollback_transaction to be called but was not")
end
@@ -6,7 +6,17 @@
module ActiveRecord
module DatabaseTasksSetupper
def setup
@mysql_tasks, @postgresql_tasks, @sqlite_tasks = stub, stub, stub
@mysql_tasks, @postgresql_tasks, @sqlite_tasks = Array.new(
3,
Class.new do
def create; end
def drop; end
def purge; end
def charset; end
def collation; end
def structure_dump(*); end
end.new
)
ActiveRecord::Tasks::MySQLDatabaseTasks.stubs(:new).returns @mysql_tasks
ActiveRecord::Tasks::PostgreSQLDatabaseTasks.stubs(:new).returns @postgresql_tasks
ActiveRecord::Tasks::SQLiteDatabaseTasks.stubs(:new).returns @sqlite_tasks
@@ -7,7 +7,7 @@
module ActiveRecord
class MysqlDBCreateTest < ActiveRecord::TestCase
def setup
@connection = stub(create_database: true)
@connection = Class.new { def create_database(*); end }.new
@configuration = {
"adapter" => "mysql2",
"database" => "my-app-db"
@@ -96,15 +96,15 @@ def teardown
end
def test_raises_error
assert_raises(Mysql2::Error) do
assert_raises(Mysql2::Error, "Invalid permissions") do
ActiveRecord::Tasks::DatabaseTasks.create @configuration
end
end
end
class MySQLDBDropTest < ActiveRecord::TestCase
def setup
@connection = stub(drop_database: true)
@connection = Class.new { def drop_database(name); true end }.new
@configuration = {
"adapter" => "mysql2",
"database" => "my-app-db"
@@ -142,7 +142,7 @@ def test_when_database_dropped_successfully_outputs_info_to_stdout
class MySQLPurgeTest < ActiveRecord::TestCase
def setup
@connection = stub(recreate_database: true)
@connection = Class.new { def recreate_database(*); end }.new
@configuration = {
"adapter" => "mysql2",
"database" => "test-db"
@@ -176,7 +176,7 @@ def test_recreates_database_with_the_given_options
class MysqlDBCharsetTest < ActiveRecord::TestCase
def setup
@connection = stub(create_database: true)
@connection = Class.new { def charset; end }.new
@configuration = {
"adapter" => "mysql2",
"database" => "my-app-db"
@@ -193,7 +193,7 @@ def test_db_retrieves_charset
class MysqlDBCollationTest < ActiveRecord::TestCase
def setup
@connection = stub(create_database: true)
@connection = Class.new { def collation; end }.new
@configuration = {
"adapter" => "mysql2",
"database" => "my-app-db"
@@ -7,7 +7,7 @@
module ActiveRecord
class PostgreSQLDBCreateTest < ActiveRecord::TestCase
def setup
@connection = stub(create_database: true)
@connection = Class.new { def create_database(*); end }.new
@configuration = {
"adapter" => "postgresql",
"database" => "my-app-db"
@@ -89,7 +89,7 @@ def test_create_when_database_exists_outputs_info_to_stderr
class PostgreSQLDBDropTest < ActiveRecord::TestCase
def setup
@connection = stub(drop_database: true)
@connection = Class.new { def drop_database(*); end }.new
@configuration = {
"adapter" => "postgresql",
"database" => "my-app-db"
@@ -131,7 +131,10 @@ def test_when_database_dropped_successfully_outputs_info_to_stdout
class PostgreSQLPurgeTest < ActiveRecord::TestCase
def setup
@connection = stub(create_database: true, drop_database: true)
@connection = Class.new do
def create_database(*); end
def drop_database(*); end
end.new
@configuration = {
"adapter" => "postgresql",
"database" => "my-app-db"
@@ -179,7 +182,7 @@ def test_establishes_connection
class PostgreSQLDBCharsetTest < ActiveRecord::TestCase
def setup
@connection = stub(create_database: true)
@connection = Class.new { def create_database(*); end }.new
@configuration = {
"adapter" => "postgresql",
"database" => "my-app-db"
@@ -197,7 +200,7 @@ def test_db_retrieves_charset
class PostgreSQLDBCollationTest < ActiveRecord::TestCase
def setup
@connection = stub(create_database: true)
@connection = Class.new { def create_database(*); end }.new
@configuration = {
"adapter" => "postgresql",
"database" => "my-app-db"
@@ -69,11 +69,14 @@ def test_db_create_with_error_prints_message
class SqliteDBDropTest < ActiveRecord::TestCase
def setup
@database = "db_create.sqlite3"
@path = stub(to_s: "/absolute/path", absolute?: true)
@configuration = {
"adapter" => "sqlite3",
"database" => @database
}
@path = Class.new do
def to_s; "/absolute/path" end
def absolute?; true end
end.new
Pathname.stubs(:new).returns(@path)
File.stubs(:join).returns("/former/relative/path")
@@ -126,7 +129,7 @@ def test_when_db_dropped_successfully_outputs_info_to_stdout
class SqliteDBCharsetTest < ActiveRecord::TestCase
def setup
@database = "db_create.sqlite3"
@connection = stub :connection
@connection = Class.new { def encoding; end }.new
@configuration = {
"adapter" => "sqlite3",
"database" => @database

0 comments on commit f7bfb3d

Please sign in to comment.