[rspec-users] Suggested shorthand for and_return when stubbing

David Chelimsky dchelimsky at gmail.com
Sat Feb 20 18:22:09 EST 2010

On Sat, Feb 20, 2010 at 4:09 PM, Matt Wynne <matt at mattwynne.net> wrote:
> I keep wanting to do this, and I don't think it's possible so I thought I'd
> suggest it. Similarly to the way I can specify stub values as hash key/value
> pairs when constructing a test double, I'd like to be able to the same when
> subsequently calling stub on that double:
>    me.stub(:name => 'Matt')
> That's as opposed to
>    me.stub(:name).and_return('Matt')
> Thoughts?

Have you tried it? It already works. You can do this, in fact:

  me.stub(:name => 'David', :predictor_of_matts_future_requests => true)

Also, try this one:

  me.stub(:name) { 'Matt' }

That's actually my preference, because it gives you access to args:

  me.stub(:sum) {|a,b| a + b}
  => 7


More information about the rspec-users mailing list