【问题标题】:Call Rspec Method from Rakefile从 Rakefile 调用 Rspec 方法
【发布时间】:2018-07-13 19:30:34
【问题描述】:

我正在尝试编写一个 rake 测试,该测试需要调用另一个文件中的方法以设置我当前的会话,但是当我尝试在我的 rake 文件顶部包含文件路径时,我收到此错误:

rake aborted!LoadError: cannot load such file -- ~/../../spec/models/run_spec.rb

我的rakefile如下:

require 'sqlite3'
require 'optparse'
require '~/../../spec/models/run_spec.rb'

task refresh_gn_input_files: :environment do
     LOANS_INPUT_PATH_GN = Rails.root.to_s +
       '/spec/inputs/loans_input_gn.json'
     PRICING_INPUT_PATH_GN = Rails.root.to_s +
       '/spec/inputs/pricing_input_gn.json'
 
     puts 'creating and configuring current session...'
     create_and_configure_session
 
     puts 'generating loans input file...'
     puts "THIS IS THE TEST: #{@session}"
 
     puts 'geerating pricing input file...'
   end

所以我希望能够调用我在另一个文件中的这个“create_and_configure_session”方法,但我无法将它包含在我的 rakefile 中?

rakefile 的路径:~/Desktop/a/spec/models/run_spec.rb

外部方法的路径:~/Desktop/a/lib/tasks/a_tasks_rake

方法代码:

def create_and_configure_session 
  @support_engine = M::ScriptSet.new.get_engine("Support")
  ...
end

【问题讨论】:

  • 您希望测试和应用程序代码相互泄漏似乎很奇怪...我想这将是您需要重构某些东西的标志。 :-)

标签: ruby-on-rails ruby path rake


【解决方案1】:

在您尝试的代码中,您将从~ 上升两个级别(使用..)。

在 Mac 上,您可以访问/

其中任何一个都可以让您访问您的文件:

require_relative '../../spec/models/run_spec'
require '~/Desktop/a/spec/models/run_spec'

【讨论】:

  • 谢谢我发现我现在的问题是我不能在我的规范文件中使用我的方法,因为它需要从根调用时与 rspec 一起使用的“spec_helper”。我正在尝试创建一个单独的类
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-07
  • 2017-06-07
  • 2015-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多