Skip to content

Commit

Permalink
Handle initialize as a special case
Browse files Browse the repository at this point in the history
  • Loading branch information
godfat committed Nov 28, 2020
1 parent c302123 commit 9b1b8fe
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/muack/mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,13 @@ def __mock_super_with_kargs? object, method_name
::Kernel.instance_method(:method).bind(object).call(method_name).
super_method

super_method && __mock_block_with_kargs?(super_method)
if super_method.owner == ::Class && super_method.name == :new
initialize_method = ::Class.instance_method(:instance_method).
bind(object).call(:initialize)
__mock_block_with_kargs?(initialize_method)
else
super_method && __mock_block_with_kargs?(super_method)
end
end

def __mock_block_with_kargs? block
Expand Down
13 changes: 13 additions & 0 deletions test/test_keyargs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,17 @@ def instance
expect(Obj.say(a: 0)).eq(Obj.object_id)
end
end

would 'handle initialize via new' do
kargs_initialize = Class.new do
def initialize a:
@a = a
end
attr_reader :a
end

mock(kargs_initialize).new(a: 0)

expect(kargs_initialize.new(a: 0).a).eq(0)
end
end

0 comments on commit 9b1b8fe

Please sign in to comment.