Skip to content

Commit 3c904f2

Browse files
committed
[refactor] simplify test_helper connection closing
Also ActiveRecord::Base.connection gives an active connection which might not be the one that is connected
1 parent cfeffcf commit 3c904f2

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

test/test_helper.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,14 @@ def disable_logger(connection = self.connection, &block)
126126

127127
def with_connection(config)
128128
ActiveRecord::Base.establish_connection config
129-
yield ActiveRecord::Base.connection
129+
conn = ActiveRecord::Base.connection
130+
yield conn
130131
ensure
131-
ActiveRecord::Base.connection.disconnect!
132+
conn&.disconnect!
132133
end
133134

134135
def self.disconnect_if_connected
135-
if ActiveRecord::Base.connected?
136-
ActiveRecord::Base.connection.disconnect!
137-
ActiveRecord::Base.remove_connection
138-
end
136+
ActiveRecord::Base.clear_all_connections!
139137
end
140138

141139
def disconnect_if_connected; self.class.disconnect_if_connected end

0 commit comments

Comments
 (0)