[mocha-developer] problem with using any_instance

Yi Wen hayafirst at gmail.com
Mon Apr 2 08:42:28 EDT 2007

Thanks James, I also looked into the Mocha code. It seems in AnyInstance
class, restore_original_method uses "method_defined?" and this method
matches only public and protected methods in the module/included classes,
etc. (And method_defined? is used in many other cases as well).  I think
that's why the original private method_name was not  restored.

stubbee.class_eval "alias_method :#{method}, :#{hidden_method};
remove_method :#{hidden_method}" if stubbee.method_defined?(hidden_method)

I agree with you that stubbing a private method is not a good idea in
general. So I agree with you that what need to be done is to disallow
mocking private methods explicitly. From what I can see in a very quick
glance, "stubs" method is not guarded with any checking... Anyway, just
trying to be helpful.

And thanks again, your correspondence helped a lot.


On 4/1/07, James Mead <jamesmead44 at gmail.com> wrote:
> It looks like the critical aspect of your failing test is the fact that
> you
> are stubbing a private method.
> Originally Mocha did not allow stubbing of private methods. Somehow
> accidentally it became allowed. Stubbing private methods isn't a good
> idea,
> because it couples the test to the internals of the class.
> There seems to be a bug with stubbing a private method. I'll look into it,
> but in the meantime you can fix your test if you are able to make the
> stubbed method public.
> Thanks.
> --
> James.
> http://blog.floehopper.org
> _______________________________________________
> mocha-developer mailing list
> mocha-developer at rubyforge.org
> http://rubyforge.org/mailman/listinfo/mocha-developer

More information about the mocha-developer mailing list