Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pass_thru behavior when the proc is spec-ed via a Proc as last argument #27

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 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,20 @@ def pass_thru(&block)
block ||= lambda { |value| value }
and_return { |*args, &orig_block|
begin
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
Loading