Uko
Uko

Reputation: 13396

Define a variable in group and use in test declarations

I'm writing tests for my rails app and I've found out that as almost an the tests are run on the set of some n variables, I can write them as follows:

[var1, var2, …].each do   
    describe … do
     .
     .
    end

    it … do
     .
     .
    end
end

and so on. But I can't find a way how to put all that array into a variable, so I can just use variables.each do ….

I've tried before(:each), before(:all) and just a declaration of and instance variable (one that starts with @-sign) in a describe group, but all my tries were useless.

Upvotes: 0

Views: 132

Answers (1)

Samy Dindane
Samy Dindane

Reputation: 18726

Declare variables in the Rspec.configure block:

RSpec.configure do |config|
  config.add_setting :variables, :default =>  [1, 2]
end

And use it inside your specs like this:

RSpec.configuration.variables.each do |i|
end

You can also declare variables outside of the RSpec configuration block like in the example below, but since you probably already have this block, better keep things clean.

RSpec.configuration.add_setting :variables, :default =>  [1, 2]

Upvotes: 1

Related Questions