#4 new
James Mead

[#10412] Merge 'with' and the expected method

Reported by James Mead | January 1st, 2009 @ 06:09 PM

Date: 2007-04-27 16:33 Opener: Brian Takita

It seems that the 'with' method can be merged with the method name expectation. This would make mocks shorter and easier to read for some people.

Here are a couple of examples with their equivalent current syntax:


the_mock.expects.foo(1,2) {|a, b| :bar}
the_mock.expects(:foo).with(1,2) {|a, b| :bar}

the_mock.expects.foo(1,2).returns(:bar)
the_mock.expects.(:foo).with(1,2).returns(:bar)

Also, please look at the mock block feature request. WDYT?

Comments and changes to this ticket

  • James Mead

    James Mead January 1st, 2009 @ 06:11 PM

    • Tag set to feature-request

    Date: 2007-05-02 09:20 Sender: Brian Takita

    I am posting a patch that implements this. It's actually pretty simple.

  • James Mead

    James Mead January 1st, 2009 @ 06:12 PM

    Date: 2007-10-31 14:54 Sender: Steve Purcell

    I agree. I implemented this feature in my own (otherwise inferior) precursor to Mocha, called mockr (http://dev.sanityinc.com/mockr/)...>

    Before seeing this ticket I sat down to add this "pseudo call" behaviour to Mocha. The attached patch implements it in what I believe is a simpler and less intrusive way.

    Note that I do not support passing block-based parameter constraints, such as in the first of Brian's examples, since I believe it can be confusing -- Brian's second example is the compelling day-to-day case.

  • James Mead

    James Mead January 1st, 2009 @ 06:12 PM

    Date: 2007-10-31 15:00 Sender: Steve Purcell

    Okay, so I'm not permitted to attach a file to this ticket, so I've filed my patch: http://rubyforge.org/tracker/ind... ("Patches" section of RubyForge "Tracker" for this project.)

  • James Mead

    James Mead January 1st, 2009 @ 06:15 PM

    Date: 2007-11-05 10:06 Sender: James Mead

    Hi Steve,

    Thanks for taking the time to submit a patch. I agree with you about Brian's second example being the more compelling.

    There's clearly a trade-off between adding extra complexity to the Mocha code-base and providing an alternative API. This is a judgement call and depends somewhat on the perceived usefulness of the alternative API. While I like the expected method call looking more like the actual call, I don't particularly like the dot between the expects and foo.

    I have some other ideas about this, but in the meantime I'll take a look at your patch and see how much complexity it adds. You might want to look at Brian's rubyforge project - http://rubyforge.org/projects/mo... which is a mini fork of Mocha.

    Thanks again for your interest.

    Cheers, James. http://blog.floehopper.org

  • James Mead

    James Mead January 1st, 2009 @ 06:18 PM

    Date: 2007-10-31 14:59 Sender: Steve Purcell

    This small patch with tests essentially implements feature request http://rubyforge.org/tracker/ind..., which allows for a nice alternate syntax for setting up expectations and stubs. Using this syntax, the following lines are equivalent

    
    mock.expects(:some_method).with(1,2,"three")
    mock.expects.some_method(1,2,"three")
    

    (The same goes for mock.stubs)

    The Rubyforge tracker wouldn't let me attach my patch to ticket http://rubyforge.org/tracker/ind..., hence this new tracker item.

    -Steve

  • James Mead

    James Mead January 1st, 2009 @ 06:18 PM

    Date: 2007-11-03 13:22 Sender: Steve Purcell

    Here's an updated version of the patch which omits changes to the trailing white space present in the svn version of the file.

  • James Mead

    James Mead January 1st, 2009 @ 06:19 PM

    Date: 2007-11-05 10:07 Sender: James Mead

    Hi Steve,

    I've responded over on the feature request...

    http://rubyforge.org/tracker/ind...

    Cheers, James. http://blog.floehopper.org

  • James Mead

    James Mead January 1st, 2009 @ 06:22 PM

    Date: 2007-11-05 10:21 Sender: Steve Purcell

    James: For some reason my trimmed patch didn't get uploaded when I made the prior comment; here it is. I personally think it's not particularly invasive, but I'm glad you're the ultimate arbiter of good taste.

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

Pages