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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With