开发者

RSpec 2 before(:suite) variable scope

开发者 https://www.devze.com 2023-02-17 02:51 出处:网络
Basically I\'d like to create an array and then append to it during my specs before finally processing and displaying it to the user. I can come up with a few workarounds but ideally I\'d like to do t

Basically I'd like to create an array and then append to it during my specs before finally processing and displaying it to the user. I can come up with a few workarounds but ideally I'd like to do the following.

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

Unfortunately but not surprisingly @array_of_stuff isn't in scope and can't be appended to from my specs, unlike if setup in a before(:all) block.

Is there something RSpe开发者_C百科c provides that would make something like this very straightforward?


It was probably not intended for this, but you can use custom settings:

spec_helper:

RSpec.configure do |config|
  config.add_setting :my_array
  config.before(:suite) { RSpec.configuration.my_array = [] }
end

example spec:

it "should do something" do
  RSpec.configuration.my_array << "some value"
  RSpec.configuration.my_array.length.should eql(1)
end
0

精彩评论

暂无评论...
验证码 换一张
取 消