[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")
).returns(from_block)

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 }
end

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|
    file.read
    puts "Stolid in my guilt, clutching it like a girl with a stuffed
bunny, I failed to follow."
  end
end

-- 
Jonathan Leighton, Web Developer
http://jonathanleighton.com/



More information about the mocha-developer mailing list