views:

39

answers:

2

I have a scenario more or less like this

class A
  def initialize(&block)
    b = B.new(&block)
  end
end

I am unit testing class A and I want to know if B#new is receiving the block passed to A#new. I am using Mocha as mock framework.

Is it possible?

A: 

I think you want:

l = lambda {}
B.expects(:new).with(l)
A.new(&l)

I know this works with RSpec, I'd be surprised if Mocha doesn't handle

Bryan Ash
Didn't work. I am starting to believe I cant do that in this way. I will try another approach here. Thanks! :)
rafaelss
+1  A: 

I tried this with both Mocha and RSpec and although I could get a passing test, the behavior was incorrect. From my experiments, I conclude that verifying that a block is passed is not possible.

Question: Why do you want to pass a block as a parameter? What purpose will the block serve? When should it be called?

Maybe this is really the behavior you should be testing with something like:

class BlockParamTest < Test::Unit::TestCase

  def test_block_passed_during_initialization_works_like_a_champ
    l = lambda {|name| puts "Hello #{name}"}
    l.expects(:call).with("Bryan")
    A.new(&l) 
  end

end
Bryan Ash