2013-10-24 19 views
0

我創建了獨立的rspec測試腳本來測試現有的api框架。它工作得很好,但我發現問題,即在Rake文件我需要分配從YAML文件中的一些值(URI鏈接,電子郵件)或者CONSTANT$global_var在Rake文件的代碼如下所示:rake spec失敗,無法加載或查找常量或全局變量

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

如果opt[:choice]既不是"production"也不是"development",VAR未在您的代碼中定義。

+0

請檢查'opt [:choice]'或'choice'的值。 – iltempo

+0

opt [:choice]或選擇沒有顯示值,它只顯示空的散列。 我只是想;這裏有兩個mo值。 – mcbuddy

+0

是的。在這種情況下'VAR'沒有被定義。 – iltempo