我创建了独立的 rspec 测试脚本来测试现有的 api 框架。它工作得很好,但我发现在 Rakefile 中我需要从 YAML 文件(uri 链接、电子邮件)中分配一些值CONSTANT
或者$global_var
Rakefile 中的代码如下所示的问题:
require 'rubygems'
require 'bundler/setup'
require 'yaml'
require 'rspec/core/rake_task'
task :default => :spec
desc 'Running rspec test'
task :spec, :option do |t, opt|
choice = opt[:choice]
if choice == "production"
puts 'Test running on production'
VAR = YAML::load(File.read(File.expand_path("../config/prod_variable.yml", __FILE__)))
elsif choice == "development"
puts 'Test running on development'
VAR = YAML::load(File.read(File.expand_path("../config/dev_variable.yml", __FILE__)))
end
puts VAR['URI'] #=> print out the value correctly
RSpec::Core::RakeTask.new do |task|
test = Rake.application.original_dir
task.fail_on_error = false
task.rspec_opts = '--format documentation --color'
end
end
当我在终端上运行 rake 命令时,rspec 找不到VAR
常量值。这是来自 rspec 的错误消息
Failures:
1) ApiTest Testing API platform for GET request
Failure/Error: @var = ApiTest.new(VAR['URI'] ,
NameError:
uninitialized constant VAR
# ./rspec_test/api_test/api_test_get_spec.rb:8:in `block (2 levels) in <top (required)>'
2) ApiTest Testing API platform for POST request
Failure/Error: @zat = ApiTest.new(VAR['URI'] ,
NameError:
uninitialized constant VAR
# ./rspec_test/api_test/api_test_post_spec.rb:7:in `block (2 levels) in <top (required)>'
有什么想法如何得到这个作品吗?我需要从 VAR 常量或全局变量中获取值,但似乎 ruby 未能分配该值。