Skip to content

Commit

Permalink
Fix stubbing method with arguments in `activerecord/test/cases/explai…
Browse files Browse the repository at this point in the history
…n_test.rb`
  • Loading branch information
fatkodima committed Feb 2, 2023
1 parent 41b5306 commit 1aba4b0
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions activerecord/test/cases/explain_test.rb
Expand Up @@ -64,10 +64,21 @@ def test_exec_explain_with_binds
end

private
def stub_explain_for_query_plans(query_plans = ["query plan foo", "query plan bar"], &block)
def stub_explain_for_query_plans(query_plans = ["query plan foo", "query plan bar"])
explain_called = 0

connection.stub(:explain, proc { explain_called += 1; query_plans[explain_called - 1] }, &block)
# Minitest's `stub` method is unable to correctly replicate method arguments
# signature, so we need to do a manual stubbing in this case.
metaclass = class << connection; self; end
explain_method = metaclass.instance_method(:explain)
metaclass.define_method(:explain) do |_arel, _binds = [], _options = {}|
explain_called += 1
query_plans[explain_called - 1]
end
yield
ensure
metaclass.undef_method(:explain)
metaclass.define_method(:explain, explain_method)
end

def bind_param(name, value)
Expand Down

0 comments on commit 1aba4b0

Please sign in to comment.