Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec 2 before(:suite) variable scope

Tags:

ruby

rspec

rspec2

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 RSpec provides that would make something like this very straightforward?

like image 296
lebreeze Avatar asked Mar 15 '11 13:03

lebreeze


1 Answers

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
like image 179
Robert Speicher Avatar answered Oct 14 '22 08:10

Robert Speicher