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

Fixing an issue with a spoiling of the object singleton classes #891

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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: 6 additions & 3 deletions lib/minitest/mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,13 @@ class Object

def stub name, val_or_callable, *block_args, **block_kwargs, &block
new_name = "__minitest_stub__#{name}"
singleton_has_stubbing_method = singleton_methods.map(&:to_s).include?( name.to_s )

metaclass = class << self; self; end

if respond_to? name and not methods.map(&:to_s).include? name.to_s then
# this mean that 'name' is a public method which is expected
# to be defined dynamically somewhere inside ancestors chain
metaclass.send :define_method, name do |*args, **kwargs|
super(*args, **kwargs)
end
Expand Down Expand Up @@ -320,8 +323,8 @@ def stub name, val_or_callable, *block_args, **block_kwargs, &block

block[self]
ensure
metaclass.send :undef_method, name
metaclass.send :alias_method, name, new_name
metaclass.send :undef_method, new_name
metaclass.send :remove_method, name
metaclass.send :alias_method, name, new_name if singleton_has_stubbing_method
metaclass.send :remove_method, new_name
end
end
35 changes: 35 additions & 0 deletions test/minitest/test_minitest_mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,41 @@ def test_mock_with_yield_kwargs

alias test_stub_value__old test_stub_value # TODO: remove/rename

def test_instance_singleton_class_is_not_dirty
_class = Class.new do
def clean; :dirty end
end

_module = Module.new do
def clean; :clean end
end

shirt, t_shirt = _class.new, _class.new

assert_equal( t_shirt.clean, :dirty )
assert_equal( shirt.clean, :dirty )

shirt.stub(:clean, :clean) do |s|
assert_equal( s.clean, :clean )
end

_class.prepend(_module)

assert_equal( t_shirt.clean, :clean )
assert_equal( shirt.clean, :clean )
end

def test_class_singleton_class_will_not_loose_its_method
_class = Class.new do
def self.clean; :dirty end
end

_class.stub(:clean, :clean) do |s|
assert_equal( s.clean, :clean )
end
# method is still present and unchanged
assert_equal( _class.clean, :dirty )
end
## Permutation Sets:

# [:value, :lambda]
Expand Down