Phlip wrote:
>    def test_block
>      and_the_block_got_called = false
>      method do |x|
>        assert x == 42
>        and_the_block_got_called = true
>      end
>      assert and_the_block_got_called
>    end
> 
> How to DRY that??

Given that you're concerned in testing the number of times the block is 
invoked, then I would turn the block into a mock object which verifies 
that for you. Proof of concept:

require 'rubygems'
require 'mocha'
require 'test/unit'

class TestBlock < Test::Unit::TestCase
  def yields_once(mname, *args)
    blk = mock('block')
    blk.expects(:call).with { |*x| yield *x; true }
    send(mname, *args) { |*x| blk.call(*x) }
  end

  def my_method
    yield 42
    #yield 42
  end

  def test_my_method
    yields_once(:my_method) do |x|
      assert_equal 42, x
    end
  end
end

This gives an informative error if the block is not called, or is called 
two or more times:

  1) Failure:
test_uses_block(TestBlock)
    [ert.rb:8:in `yields_once'
     ert.rb:18:in `test_uses_block']:
not all expectations were satisfied
unsatisfied expectations:
- expected exactly once, not yet invoked: #<Mock:block>.call()

1 tests, 1 assertions, 1 failures, 0 errors
-- 
Posted via http://www.ruby-forum.com/.