How to define a simple global variable in an rspec test that can be accesed by helper functions

筅森魡賤 提交于 2019-12-01 04:15:23

Consider using a global before hook with an instance variable: http://www.rubydoc.info/github/rspec/rspec-core/RSpec/Core/Configuration

In your spec_helper.rb file:

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

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

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

It turns out the easiest way is to use a $ sign to indicate a global variable.

See Preserve variable in cucumber?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!