gpt4 book ai didi

Rspec - config.before(:suite) not running in spec_helper

转载 作者:行者123 更新时间:2023-12-04 08:26:40 28 4
gpt4 key购买 nike

我目前正在寻找在内存中设置一个可以在我的整个套件中使用的哈希
我正在寻找执行以下操作

RSpec.configure do |config|
config.before(:suite) { $user_tokens = initialize_my_stuff }
end
但是,当我去运行我的套件时,我从我的一个规范中收到一个错误消息: NoMethodError: undefined method 'each' for nil:NilClass它试图运行这个:
$user_tokens.each do |user,token|
describe 'foo bar' do
...
end
end
如果我注释掉这个规范, before(:suite)按预期运行。
有什么办法可以确保 before(:suite)在尝试对规范执行任何操作之前阻止运行?

最佳答案

这是发生的事情:

config.before(:suite) { $user_tokens = initialize_my_stuff }
将运行 套房前 (不足为奇),但是...
$user_tokens.each do |user,token|
describe 'foo bar' do
...
end
end
.. 只是规范定义,正在发生 之前实际套件执行。
换句话说:
  describe 'foo bar' do
...
end
只是一堆保存下来以后执行的块。并且您的 $user_tokens 尚未初始化。
我建议使用 initialize_my_stuff在你的规范里面是这样的:
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

关于Rspec - config.before(:suite) not running in spec_helper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65224733/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com