Rails 3:如何在运行测试时包含“lib”目录中的文件?

时间:2011-03-10 05:21:58

标签: ruby-on-rails unit-testing ruby-on-rails-3 testing

当我跑步时:

ruby -I test test/unit/job_test.rb

从应用程序根目录,我收到以下错误:

.../app/models/name_position.rb:9:in `<class:NamePosition>': 
     uninitialized constant NamePosition::PositionManager (NameError)
from .../app/models/name_position.rb:1:in `<top (required)>'
from .../app/models/ac_buyer.rb:1:in `<top (required)>'
...  

以下是相关代码:

# app/models/ac_buyer.rb
class AcBuyer < NamePosition  
end 

# app/models/name_position.rb
class NamePosition < ActiveRecord::Base
  ...
  include PositionManager
end

# lib/position_manager.rb
module PositionManager
  ...
end

看起来在运行测试时,lib目录中的文件不会自动加载。

有什么建议吗?

1 个答案:

答案 0 :(得分:6)

尝试将其放入您的application.rb

config.autoload_paths += %W(#{config.root}/lib)