Layout methods that receive a block as a parameter

I have a script more like this

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

I am testing a class A module and I want to know if B # new is getting the block passed to # new. I use Mocha as a layout.

Is it possible?

+5
source share
3 answers

I tried this with both Mocha and RSpec, and although I could pass the test, the behavior was wrong. From my experiments, I came to the conclusion that checking that the block was transferred is not possible.

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

Perhaps this is really a behavior that you should test 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
+2

, :

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

, RSpec, , Mocha

0

Is it a B::newconcession to the block and does the block change the parameter that yieldprovides? If so, then you can test it as follows:

require 'minitest/autorun'
require 'mocha/setup'

describe 'A' do

  describe '::new' do

    it 'passes the block to B' do
      params = {}
      block = lambda {|p| p[:key] = :value }
      B.expects(:new).yields(params)

      A.new(&block)

      params[:key].must_equal :value
    end

  end

end
0
source

All Articles