Mock object, object_id and ruby debugger

78 views
Skip to first unread message

spilled_coffee

unread,
Nov 8, 2012, 1:14:04 PM11/8/12
to mocha-d...@googlegroups.com
Hi,

Many of our objects override the id method returning non-integer value and so mock objects for them would need to stub the id method too. The debugger will test if the object respond_to?(:object_id) and if not call the id method to get the object id. The problem is that the ruby debugger (or some versions e.g. 0.4.16) has print_variable (in C codes) that expects integer values. But while Mocha mock object does serve the object_id method by the virtue of being an Object, its overridden respond_to?() returns false for :object_id. I try a monkey-patch that would solve the problem:

# To make debugging spec test with stubbed :id objects in debugger possible.
require 'mocha/mock'
Mocha::Mock.class_eval <<-MOCHA
        #alias_method :mock_respond_to, :respond_to?
        #def respond_to?(symbol, include_private = false)
        #  mock_respond_to(symbol, include_private) || symbol == :object_id
        #end
        # the above approach would cause broken debugger loop due to error ''Exception in DebugThread loop: unexpected invocation: #<Mock:image1>.mock_respond_to(:object_id, false)''
        # so we have to resort to copying codes
        def respond_to?(symbol, include_private = false)
          return true if symbol == :object_id
          if @responder then
            if @responder.method(:respond_to?).arity > 1
              @responder.respond_to?(symbol, include_private)
            else
              @responder.respond_to?(symbol)
            end
          else
            @everything_stubbed || @expectations.matches_method?(symbol)
          end
        end
MOCHA

My questions are:
  1. Is there a better way to deal with this?
  2. If not, should the mocha include this in an official update?
  3. Is there a way to use the alias approach in the above monkey-patch so we don't have to copy a block of important codes?

Thanks

floehopper

unread,
Feb 23, 2013, 8:16:21 AM2/23/13
to mocha-d...@googlegroups.com

Hi.

Sorry it's taken so long to reply. This message somehow ended up in my spam filter.

I've added an issue [1] on Github for your question.

Regards, James.

[1] https://github.com/freerange/mocha/issues/139

Reply all
Reply to author
Forward
0 new messages