Skip to content
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
9 changes: 7 additions & 2 deletions lib/flexmock/partial_mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def initialize_stub(recorder, expectations_block)
expectation_blocks = @initialize_expectation_blocks = Array.new
expectation_recorders = @initialize_expectation_recorders = Array.new
@initialize_override = Module.new do
define_method :initialize do |*args, &block|
define_method :initialize do |*args, **kw, &block|
if self.class.respond_to?(:__flexmock_proxy) && (mock = self.class.__flexmock_proxy)
container = mock.flexmock_container
mock = container.flexmock(self)
Expand All @@ -287,7 +287,12 @@ def initialize_stub(recorder, expectations_block)
r.apply(mock)
end
end
super(*args, &block)
if kw.empty?
# Workaround kw arg support for ruby < 2.7
super(*args, &block)
else
super(*args, **kw, &block)
end
end
end
override = @initialize_override
Expand Down
7 changes: 5 additions & 2 deletions test/new_instances_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ def self.make

class Cat
attr_reader :name
def initialize(name, &block)
attr_reader :kw
def initialize(name, **kw, &block)
@name = name
@kw = kw
block.call(self) if block_given?
end
end
Expand Down Expand Up @@ -94,10 +96,11 @@ def test_new_instances_will_pass_args_to_new
obj.should_receive(:meow).and_return(:scratch)
end
x = :not_called
m = Cat.new("Fido") { x = :called }
m = Cat.new("Fido", a: 42) { x = :called }
assert_equal :scratch, m.meow
assert_equal "Fido", m.name
assert_equal :called, x
assert_equal({ a: 42 }, m.kw)
end

# Some versions of the software had problems invoking the block after a
Expand Down