How to define a simple global variable in the rspec test that auxiliary functions can join

I cannot figure out how to use a simple global variable in the rspec test. It seems like such a trivial feature, but after many problems I could not find a solution.

I want a variable that can be accessed / modified in the entire main specification file and from the functions in the auxiliary specification files.

Here is what I still have:

require_relative 'spec_helper.rb' require_relative 'helpers.rb' let(:concept0) { '' } describe 'ICE Testing' do describe 'step1' do it "Populates suggestions correctly" do concept0 = "tg" selectConcept() #in helper file. Sets concept0 to "First Concept" puts concept0 #echos tg?? Should echo "First Concept" end end 

.

  #helpers.rb def selectConcept concept0 = "First Concept" end 

Can someone point out what I am missing, or if using "let" is this a completely wrong method?

+6
source share
2 answers

Using an instance variable, consider using global before hook: http://www.rubydoc.info/github/rspec/rspec-core/RSpec/Core/Configuration

In spec_helper.rb file:

 RSpec.configure do |config| config.before(:example) { @concept0 = 'value' } end 

Then, in your examples, @ concept0 (my_example_spec.rb) will be indicated:

 RSpec.describe MyExample do it { expect(@concept0).to eql('value') } # This code will pass end 
+7
source

It turned out that the easiest way is to use the $ sign to denote a global variable.

See Save Variable in Cucumber?

+5
source

Source: https://habr.com/ru/post/955209/


All Articles