RSpec 2之前(:套件)变量范围

时间:2011-03-15 13:21:18

标签: ruby rspec rspec2

基本上我想创建一个数组,然后在我的规范中追加它,最后处理并显示给用户。我可以提出一些解决方法,但理想情况下我想做以下几点。

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)块中设置。

RSpec提供的东西是否会使这样的事情变得非常简单?

1 个答案:

答案 0 :(得分:9)

可能不是这样,但您可以使用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