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

John Wilger johnwilger at gmail.com
Wed Jul 18 14:21:36 EDT 2007

On 7/18/07, James Mead <jamesmead44 at gmail.com> wrote:
> I'm a bit confused as to what you are trying to test - there are no expects
> or asserts in your examples. Can you give a simple but complete example with
> both test and code. In particular, are you trying to test the functionality
> of the method or the block?

I think he's trying to test the following type of thing (at least,
this is how I read it):

# test case method
def test_should_offset_each_char_by_one_position
  scrambler = Scrambler.new
  File.expects( :open ).with( '/abc.txt', 'r' ).yields( stub( :read => 'abc' ) )
  assert_equal 'bca', scrambler.offset_read( 'abc.txt' )

# Scrambler method
def offset_read( path )
  File.open( path, 'r' ) do |f|
    chars = file.read.to_a
    ( chars << chars.shift ).to_s

In other words, if you have an expectation which uses #yields, rather
than having to explicitly define the return value, is it possible to
have it return the result of the block that was yielded to? Maybe this
already works -- I haven't tried.

John Wilger

"Quality means doing it right when no one is looking." – Henry Ford

More information about the mocha-developer mailing list