diff --git a/lib/flexmock/partial_mock.rb b/lib/flexmock/partial_mock.rb index 1dbf7db..0451480 100644 --- a/lib/flexmock/partial_mock.rb +++ b/lib/flexmock/partial_mock.rb @@ -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) @@ -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 diff --git a/test/new_instances_test.rb b/test/new_instances_test.rb index 8397ff5..8eef776 100644 --- a/test/new_instances_test.rb +++ b/test/new_instances_test.rb @@ -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 @@ -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