Ruby Metaprogramming- 동적으로 추가 된 방법에서 작동하지 않음
-
05-07-2019 - |
문제
나는 연장을 위해 노력하고있다 Notamock RSPEC에서 스터 빙 방법을위한 프레임 워크 및 스터브가 메서드 블록에 수율을 얻는다.
그만큼 이 요점에서 코드 내가 스스로 코딩 할 때 완벽하게 작동합니다 (Notamock Stubs Methods와 비슷한 방법).
그러나 객체를 통합 할 때 instance_eval ... 코드를 notamock 프레임 워크, "block_given?" 항상 거짓을 반환하면 그 때문에 내 수익률을 얻을 수 없습니다. 이 방법이 올바르게 추가되고 스터브 베드 메소드를 호출 할 수 있지만 Notamock Stubbed 버전에서 메소드로 전달한 블록을 인식하지 못합니다.
이 코드를 Notamock 프레임 워크에 어떻게 통합했는지 확인하려면 Notamock의 클론 개인 메소드에서 "add_hook"메소드를 확인하십시오.
나는 이것이 조금 많은 것을 알고 있습니다 ... 나는 약간의지도를 찾고 싶습니다. 그것은 하루 종일 나를 미쳤다.
해결책 2
Turns out the issue is unrelated to the code that i linked to... the NotAMock framework is doing some additional method re-definition that is causing my issue. I've narrowed the issue down to one specific line of code, and am working out how to fix it now.
다른 팁
I'm not even sure this is possible. New Procs don't even recognize blocks.
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 is supposed to allow blocks to take &block as parameters. But I don't know if that will work or even if it will allow conditional blocks.