Skip to content

Commit

Permalink
fix: pass_thru behavior when the proc is spec-ed via a Proc as last a…
Browse files Browse the repository at this point in the history
…rgument

v2.4.1 broke backward compatibility in the following case:

   with(some, arguments, Proc).pass_thru

In that case, the block would be given as positional argument to the
original method, instead of being passed as a block.
  • Loading branch information
doudou committed Sep 14, 2024
1 parent 4e3ff4d commit 2e7f854
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/flexmock/expectation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def return_value(args, block)

if @expected_block
ret_block.call(*args, &block)
elsif block
elsif block && @expected_block.nil?
ret_block.call(*args, block)
else
ret_block.call(*args)
Expand Down Expand Up @@ -407,14 +407,21 @@ def pass_thru(&block)
block ||= lambda { |value| value }
and_return { |*args, &orig_block|
begin
puts "PASS_THRU #{args} #{orig_block}"
if @expected_block.nil? && !orig_block
if Proc === args.last
orig_block = args.last
args = args[0..-2]
end
end
block.call(@mock.flexmock_invoke_original(@sym, args, orig_block))
rescue NoMethodError => e
if e.name == @sym
raise e, "#{e.message} while performing #pass_thru in expectation object #{self}"
else
raise
end
end
end
}
end

Expand Down
14 changes: 14 additions & 0 deletions test/partial_mock_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,20 @@ def mocked_method(&block)
assert_equal block, obj.block
end

def test_pass_thru_forwards_a_block_if_there_is_no_explicit_absence
obj = Class.new do
attr_reader :block
def mocked_method(&block)
@block = block
end
end.new
flexmock(obj).should_receive(:mocked_method).pass_thru

block = obj.mocked_method { 42 }
assert_kind_of Proc, block
assert_equal block, obj.block
end

def test_it_checks_whether_mocks_are_forbidden_before_forwarding_the_call
obj = Class.new
flexmock(obj).should_receive(:mocked).never
Expand Down

0 comments on commit 2e7f854

Please sign in to comment.