1

I'm working on extending the NotAMock framework for stubbing methods in rspec, and getting the stubs to yield to a methods block.

The code in this Gist works perfectly when I code it on my own (which is done-up to resemble how NotAMock stubs methods).

but when I incorporate the object.instance_eval... code into the NotAMock framework, the "block_given?" always returns false and I can never get my yield to work because of that. The method is added correctly, and I can call the stubbed method... but it will not recognize the block that i pass to the method, from the NotAMock stubbed version.

To see how i have incorporated this code into the NotAMock framework, go to my clone of NotAMock and check out the "add_hook" method in the private methods.

I know this is a bit much to ask... i'm hoping to find some guidance. it's been driving me nuts all day.

4

2 に答える 2

2

これが可能かどうかさえわかりません。新しい Proc はブロックを認識しません。

proc = Proc.new do
  if block_given?
    yield
  else
    puts "Not in a block"
  end
end

proc.call { puts "In a block"} # => puts Not in a block

Ruby 1.9 では、ブロックがパラメーターとして &block を取ることができるようになっています。しかし、それが機能するかどうか、または条件付きブロックが許可されるかどうかさえわかりません。

于 2009-10-23T04:47:57.583 に答える
0

この問題は、私がリンクしたコードとは無関係であることが判明しました... NotAMock フレームワークは、私の問題を引き起こしている追加のメソッドの再定義を行っています。問題を特定の 1 行のコードに絞り込み、現在修正方法を検討中です。

于 2009-10-23T13:59:03.717 に答える