在rails中运行无头茉莉花测试时出错

时间:2013-10-21 21:01:42

标签: ruby-on-rails ruby-on-rails-3.2 jasmine jasmine-jquery

我已经设置了一个rake任务,可以在构建服务器上运行无头茉莉花测试,并以junit格式输出结果。这是任务:

namespace :jasmine do                                                                             
  desc "Runs Jasmine tests headlessly and writes out junit xml."                                  
  task :headless_junit do |t, args|                                                               
    run_jasmine_tests(Dir.pwd)                                                                    
  end                                                                                             
end                                                                                               

def run_jasmine_tests(output_dir)                                                                 
  require 'headless'                                                                              
  require 'jasmine'                                                                               
  require 'rspec'                                                                                 
  require 'rspec/core/rake_task'                                                                  

  output_file = "#{output_dir}/jasmine_results.xml"                                               

  Headless.ly do                                                                                  
    RSpec::Core::RakeTask.new(:jasmine_continuous_integration_runner) do |t|                      
      t.rspec_opts = ['--format', 'RspecJunitFormatter', '--out', output_file ]                   
      t.verbose = true                                                                            
      t.rspec_opts += ["-r #{File.expand_path(File.join(::Rails.root, 'config', 'environment'))}"]
      t.pattern = [Jasmine.runner_filepath]                                                       
    end                                                                                           

    Rake::Task['jasmine_continuous_integration_runner'].invoke                                    
  end                                                                                             

end                                                                                               

当我运行此操作时,我收到此错误:

TypeError: jasmine.getEnv(...).currentSpec is null in http://localhost:34002/assets/jquery.js?body=true (line 1129)
expect@http://localhost:34002/assets/jquery.js?body=true:1129                                                      
@http://localhost:34002/__spec__/activity_catalog_search_filters_spec.js:15                                        
jasmine.Block.prototype.execute@http://localhost:34002/__jasmine__/jasmine.js:1064                                 
jasmine.Queue.prototype.next_@http://localhost:34002/__jasmine__/jasmine.js:2096
jasmine.Queue.prototype.next_/onComplete/<@http://localhost:34002/__jasmine__/jasmine.js:2086
... LOTS MORE ...

我正在使用rails 3.2.13,jasmine 1.3.2,headless 1.0.1,rspec 2.14.1和Jasmine-jQuery 1.5.8

我认为这可能类似于这家伙所遇到的问题: TypeError: jasmine.getEnv().currentSpec is null

1 个答案:

答案 0 :(得分:1)

事实证明,问题在于使用jQuery.get将网址加载到dom中的测试。一个空字符串作为url传递下来(因为测试编写者并不真正关心我猜的是什么)但是这导致jQuery获取当前页面(jasmine自己测试)并将其加载到dom中。随之而来的是混乱。

更有趣的事情(也许更有帮助)就是我们如何理解这一点。事实证明,花哨的佣金任务不是问题。只是无头测试使用Firefox,我通常在chrome中手动加载它们,这种错误似乎没有发生。我在Firefox中重现了错误,很容易用调试器跟踪原因。

所以最重要的是,如果您的ci测试失败并且您无法重现它,请尝试在Firefox中手动加载它们。