Skip to content

Object inspection doesn't work inside Mocks that doesn't expect the singleton_class method called #1162

@decheverria1

Description

@decheverria1

Your environment

  • ruby -v: ruby 3.3.7 (2025-01-15 revision be31f993d7) [x86_64-linux]
  • rdbg -v: rdbg 1.11.0

Describe the bug
Object inspection doesn't work inside Mocks that doesn't expect the singleton_class method called
unmocked method :singleton_class

To Reproduce
You can try to reproduce it using a minitest Mock

mock = Minitest::Mock.new

mock.expect :call, true do |*passed_args, **passed_kwargs|
  debugger
end

mock.call(123)

Or make your own mock class from scratch (of course is not the same as minitest but mimics it)

class Mock
  def initialize
    @expected = {}
  end

  def expect(name, return_value = nil, expected_args = nil, &block)
    @expected[name.to_sym] = [return_value, expected_args, block]
    self
  end

  def method_missing(name, *args, **kwargs, &blk)
    entry = @expected[name.to_sym]
    raise NoMethodError, "unmocked method #{name.inspect}, expected one of #{@expected.keys.inspect}" unless entry

    return_value , expected_args, stored_block = entry
    return stored_block.call(*args, **kwargs) if stored_block
    return_value 
  end

  def respond_to_missing?(name, include_private = false)
    @expected.key?(name.to_sym) || super
  end

  def singleton_class
    method_missing(:singleton_class)
  end
end

mock = Mock.new

mock.expect :call, true do |*passed_args, **passed_kwargs|
  debugger
end

mock.call(123)

Expected behavior
It should stop at the debugger, instead we get an error ``full_message': unmocked method :singleton_class, expected one of [:call] (NoMethodError)`

Additional context
I don't really know if this is a problem of MiniTest or from this gem, but binding.pry does not seem to fall into this error. Additionally i have identified that the code dies at lib/debug/frame_info.rb:172 where NoMethodError is not captured, although after inside safe_ispect it is captured, i don't really understand the flow of the gem but maybe we can skip the NoMethodError before safe inspecting. Lastly, i was eager to do a PR but i can't test because i'm on wsl2 and have no fast way of switching to linux at the moment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions