[mocha-developer] Mocha - dynamic method calling

James Mead jamesmead44 at gmail.com
Tue Aug 15 16:25:29 EDT 2006


Hi Dylan,

See my comments inline.

On 15/08/06, Dylan Bruzenak <dylanb at digitalvalence.com> wrote:
> I had to put in the method() change so that I could do:
>
> phone_book = mock
> phone_book.method('lookup').call('jim', 'johnson', 'tx')
>
> and have it behave as if I had just called lookup directly.

Hmm. You've definitely got a point here. I was wondering if you couldn't do
something like this...

  def test_me
    phone_book = mock()
    lookup_method = mock()
    phone_book.expects(:method).with('lookup').returns(lookup_method)
    lookup_method.expects(:call).with('jim', 'johnson', 'tx')

    phone_book.method('lookup').call('jim', 'johnson', 'tx')
  end

... but it doesn't seem to replace the "method" method as I would expect.
I'll have to look into this a bit further.

> I had to put in the responds_to stuff so that my code wouldn't just
> inject the methods; it injects accessors if they do not exist.  The
> responds to fix you suggest would fix this problem.  I'd just put both
> accessors in expectations and it would be good to go.

I intend to fix this soon as I mentioned in my last email.

> I ran into two showstoppers last night:
>
> 1. real exception stack traces are being suppressed.  When there is an
> exception anywhere in the test I seem to get an expected method not
> called message, with no stack trace for the original exception.

Can you be more specific (maybe send me the stack trace). If I have a test
like so...

  def test_me
    raise
  end

... I get the stack trace I expect. I wonder if you are getting caught out
by the auto-verify. Any method expectations you set up using "expects" must
end up being fulfilled exactly as specified before the end of a test. If you
just want to stub out the method to give a known return value, use the
"stubs" method...

  def test_fails_because_message_was_not_called
    object = mock()
    object.expects(:message).with('hello')
  end

  def test_passes_because_message_is_called
    object = mock()
    object.expects(:message).with('hello')
    object.message('hello')
   end

  def test_passes_even_though_message_is_not_called
    object = mock()
    object.stubs(:message)
  end

I realise that the stack trace is a bit confusing when an expectation is not
fulfilled - tidying that up is already on my list of things to do.

> 2. When working with multiple mocks where one is set up to expect the
> other as a method value I get my second expectation failing.  I can try
> to cook up a test case for this if you need one, but it will take me a
> while.  I'm being bull dozed with work right now.  (Who isn't ?)

Hmm. Can you come up with a simple test that fails in this way? I tried this
test and it works fine...

  def test_passes
    first_object = mock()
    second_object = mock()
    first_object.expects(:first_method).with(second_object)
    first_object.first_method(second_object)
  end

> Thanks for the prompt response and for cleaning up my comments.  I don't
> do a lot of posting actually, I'll have to get familiar with textile.

No problem. Feedback is the only way I'm going to improve Mocha. There's a
good Textile reference here <http://hobix.com/textile/>.

James.
http://blog.floehopper.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/mocha-developer/attachments/20060815/b955bf32/attachment.html 


More information about the mocha-developer mailing list