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

Jonathan Leighton j at jonathanleighton.com
Sun Jul 22 11:26:20 EDT 2007


On Wed, 2007-07-18 at 09:45 +0100, James Mead 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?

Basically, I am trying to test that my_method returns the contents of
the file. The Kernel#open method will return what its block returns, so
when stubbing Kernel#open I am trying to simulate this behaviour. The
assertion would be (sorry I missed this out):

assert_equal "The file contents", obj.my_method

Here's a complete example:

require 'test/unit'

class Foo
  def return_from_block
    return yield
  end
  
  def my_method
    return_from_block { "Something" }
  end
end

class FooTest < Test::Unit::Testcase
  def test_my_method
    foo = Foo.new
    foo.stubs(:return_from_block).returns(from_block)
    
    assert_equal "Something", foo.my_method
  end
end

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



More information about the mocha-developer mailing list