#46 open
jes5199

"expects" in a mock block doesn't return an expectation, if the parameter is a hash

Reported by jes5199 | March 16th, 2009 @ 11:29 PM

This works:


mock('Something') do
        expects( :method ).returns( :value ).at_least_once
end

but this fails:


mock('Something') do
        expects( :method => :value ).at_least_once
end

with the message:

NoMethodError: undefined method at_least_once' for {:key=>:value}:Hash

Comments and changes to this ticket

  • James Mead

    James Mead May 17th, 2009 @ 12:53 PM

    • Assigned user set to “James Mead”
  • James Mead

    James Mead May 17th, 2009 @ 01:03 PM

    Sorry - I only just saw this - I didn't have Lighthouse notifications set up correctly. I'm going to have a look at it now.

  • James Mead

    James Mead May 17th, 2009 @ 03:03 PM

    • Tag set to bug-report
    • State changed from “new” to “open”

    The second form was never intended to work, because with the Hash argument version you are potentially defining expectations for multiple methods. So it's not obvious to which expectation the at_least_once clause would apply :-

    mock('Something') do
      expects(:method_1 => 1, :method_2 => 2).at_least_once
    end
    

    However, I do think the error message should be clearer.

    I suppose it would be possible for the at_least_once to apply to all expectations, but I need to think about this a bit more.

    Cheers, James.

  • tomm

    tomm July 24th, 2009 @ 04:26 PM

    -1 on allowing at_least_once to apply to >1 expectation. The point of expectation IMO is clarity even to the level of non-developers, and I think the ambiguity will cause confusion.

  • James Mead

    James Mead July 24th, 2009 @ 06:38 PM

    I think one way to improve this is for Mock#expects to return an instance of an ExpectationList when it is passed a Hash (instead of returning the Hash which is what it currently does). This seems roughly equivalent to returning an instance of an Expectation when it is passed a single method name (which is what it currently does).

    Having made this change it would then be possible to add methods like Expectation#at_least_once to ExpectationList and have them either report a sensible error message or apply to all expectations in the list.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

A mocking & stubbing library for Ruby.

* <a href="http://github.com/floehopper/mocha">GitHub repository</a>
* <a href="http://mocha.rubyforge.org">Documentation</a>
* <a href="http://groups.google.com/group/mocha-developer">Mailing List</a>

People watching this ticket

Tags

Pages