我正在尝试做一个 DSL,其中用户可以传递一个块并期望@arg定义一个实例变量。这是一个单元测试失败的完整示例:
# Implementation
class Filter
def initialize
@arg = 'foo'
end
def self.filters &block
define_method :filter do |els|
els.select &block
end
end
end
# Usage
class Foo < Filter
filters {|el| el == @arg}
end
# Expected behavior
describe 'filters created with the DSL' do
subject { Foo.new }
it 'can use @arg in the filters block' do
els = %w[notthearg either foo other]
expect(subject.filter els).to be_eql(['foo'])
end
end
在块内使用pry或放置puts语句,我可以看到它@arg是 nil。但是Foo.new.instance_variable_get :@arg正确输出foo,所以它必须与一些范围规则有关。
我需要在实现中进行哪些更改才能使测试通过并使 DSL 正常工作?