[mocha-developer] Set the return value to "same as block"

Jonathan Leighton j at jonathanleighton.com
Mon Jul 16 17:39:09 EDT 2007

It would be good to be able to set the return value to be the same as
the return value of the block. I am trying to test opening a file in a
block and reading from it. It would be nice to use code like this:

object.stubs(:open).with("/path/to/file", "r").yields(
  stub(:read => "The file contents")

Which would make open() take the return value of the block. So this app
code would work:

def my_method
  open("/path/to/file", "r") { |file| file.read }

and this wouldn't work (because the return value of the block is not the
contents of the file):

def my_method
  open("/path/to/file", "r") do |file|
    puts "Stolid in my guilt, clutching it like a girl with a stuffed
bunny, I failed to follow."

Jonathan Leighton, Web Developer

More information about the mocha-developer mailing list