我创建独立的rspec测试脚本来测试现有的api框架。它工作得很好,但我发现问题在Rakefile中我需要从YAML文件(uri链接,电子邮件)中分配一些值CONSTANT
或$global_var
Rakefile中的代码如下所示:< / p>
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无法分配值。
答案 0 :(得分:0)
如果opt[:choice]
既不是"production"
也不是"development"
,则代码中未定义VAR
。