On 25.04.2009 14:35, Phlip wrote:
> Robert Klemme wrote:
> 
>>> Write lots of unit tests.
> 
>> PS: I have blogged about a related topic recently:
>>
>> http://blog.rubybestpractices.com/posts/rklemme/002_Writing_Block_Methods.html 
> 
> 
> Does it cover this situation?
> 
>   def test_block
>     method do |x|
>       assert x == 42
>     end
>   end
> 
> If the block doesn't call, no assertion catches that problem. You must 
> use this truly un-Ruby-like hack:
> 
>   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??


def block_test
   th = Thread.current
   th[:block_run] = false
   begin
     yield
   ensure
     raise "Block not run!" unless th[:block_run]
   end
end

def assert_block_run
   Thread.current[:block_run] = true
end

def test_block
   block_test do
     m do |x|
       assert_block_run
       assert x == 42
     end
   end
end

You can easily adjust the scheme to counting of block calls etc.

Cheers

	robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/