基本上我想创建一个数组,然后在我的规范中附加到它,然后再最终处理并将它显示给用户。我可以想出一些解决方法,但理想情况下我想执行以下操作。
RSpec.configure do |config|
config.before(:suite) { @array_of_stuff ||= [] }
config.after(:suite) { process_and_print(@array_of_stuff) }
end
def process_and_print(array)
# do stuff
end
不幸但并不令人惊讶的是@array_of_stuff 不在范围内并且不能从我的规范中追加,这与在 before(:all) block 中设置不同。
是否有 RSpec 提供的东西可以使这样的事情变得非常简单?
最佳答案
它可能不是为这个而设计的,但你可以使用 custom settings :
spec_helper:
RSpec.configure do |config|
config.add_setting :my_array
config.before(:suite) { RSpec.configuration.my_array = [] }
end
规范示例:
it "should do something" do
RSpec.configuration.my_array << "some value"
RSpec.configuration.my_array.length.should eql(1)
end
关于ruby - RSpec 2之前(:suite) variable scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5312333/