Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rspec-config.before(:suite)未在spec\u helper中运行_Rspec_Configuration_Automated Tests - Fatal编程技术网

Rspec-config.before(:suite)未在spec\u helper中运行

Rspec-config.before(:suite)未在spec\u helper中运行,rspec,configuration,automated-tests,Rspec,Configuration,Automated Tests,我目前正在寻找在内存中设置一个可以在整个套件中使用的哈希 我期待着做以下事情 RSpec.configure do |config| config.before(:suite) { $user_tokens = initialize_my_stuff } end 但是,当我去运行我的套件时,我从我的一个规范中得到一个错误,错误如下: NoMethodError:nil:NilClass的未定义方法'each' 它正在尝试运行以下命令: $user_tokens.each do |user,

我目前正在寻找在内存中设置一个可以在整个套件中使用的哈希 我期待着做以下事情

RSpec.configure do |config|
  config.before(:suite) { $user_tokens = initialize_my_stuff }
end
但是,当我去运行我的套件时,我从我的一个规范中得到一个错误,错误如下:
NoMethodError:nil:NilClass的未定义方法'each'

它正在尝试运行以下命令:

$user_tokens.each do |user,token|
  describe 'foo bar' do
    ...
  end
end
如果我注释掉这个规范,那么(:suite)之前的
将按预期运行

是否有一种方法可以确保(:suite)
块在尝试对规范执行任何操作之前运行

config.before(:suite) { $user_tokens = initialize_my_stuff }
它将在套件之前运行(毫不奇怪),但是

…只是规范定义,发生在实际套件执行之前

换言之:

  describe 'foo bar' do
    ...
  end
只是保存了一堆块以便以后执行。您的$user_令牌尚未初始化

我建议在您的规范中使用
初始化我的东西
,如下所示:

initialize_my_stuff.each do |user,token|
  describe 'foo bar' do
    ...
  end
end
或者,如果它非常昂贵,记住它:

def user_tokens
  @user_tokens ||= initialize_my_stuff
end
并使用它

user_tokens.each do |user,token|
  describe 'foo bar' do
    ...
  end
end

初始化我的东西
真的那么贵吗?你能记住它并在你的规格中使用它吗?
初始化我的东西。每个…
user_tokens.each do |user,token|
  describe 'foo bar' do
    ...
  end
end