Skip to content

Commit

Permalink
Replace shallow mocks with Ruby classes
Browse files Browse the repository at this point in the history
While preparing this I realised that some stubbed returns values
serve no purpose, so this patch drops those as well.

Step 3 in rails#33162
  • Loading branch information
utilum committed Jul 10, 2018
1 parent d41ec81 commit f7bfb3d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 18 deletions.
21 changes: 17 additions & 4 deletions activerecord/test/cases/fixtures_test.rb
Expand Up @@ -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

Expand Down
12 changes: 11 additions & 1 deletion activerecord/test/cases/tasks/database_tasks_test.rb
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions activerecord/test/cases/tasks/mysql_rake_test.rb
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down
13 changes: 8 additions & 5 deletions activerecord/test/cases/tasks/postgresql_rake_test.rb
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down
7 changes: 5 additions & 2 deletions activerecord/test/cases/tasks/sqlite_rake_test.rb
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f7bfb3d

Please sign in to comment.