使用Ruby 1.9.2的RSpec中的require lib会带来“没有此类文件可加载". [英] require lib in RSpec with Ruby 1.9.2 brings "no such file to load"
问题描述
我正在尝试将我的Rails项目之一升级到Ruby 1.9.2
.一切都进行得很好,但是一项RSpec测试失败了.在此测试中,我require
一个Ruby lib
:
I am trying to upgrade one of my Rails projects to Ruby 1.9.2
. All went pretty well, but one RSpec test broke. In this test I require
a Ruby lib
:
# file spec/models/my_lib_spec.rb
require 'spec_helper'
require 'lib/services/my_lib'
describe "MyLib" do
it "should do something" do
...
库看起来像这样:
# file lib/services/my_lib.rb
class MyLib
def self.do_something
...
在Ruby 1.8.7(REE)中,该测试运行良好:
In Ruby 1.8.7 (REE) the test worked well:
$ ruby -v
ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-darwin11.1.0], MBARI 0x6770, Ruby
Enterprise Edition 2011.03
$ rspec ./spec/models/my_lib_spec.rb
..
Finished in 1.4 seconds
2 examples, 0 failures
在Ruby 1.9.2中,我得到一个错误no such file to load
:
In Ruby 1.9.2 I get an Error no such file to load
:
$ ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.1.0]
$ rspec ./spec/models/my_lib_spec.rb
/Users/tmangner/.rvm/gems/ruby-1.9.2-p290@madgoal/gems/activesupport-
3.2.2/lib/active_support/dependencies.rb:251:in `require': no such file
to load -- lib/services/my_lib (LoadError)
我不知道,是什么让Ruby 1.9无法找到lib.
I have no clue, what keeps Ruby 1.9 from finding the lib.
推荐答案
ruby 1.9中的加载路径无法像在1.8中那样完全起作用.
The load path in ruby 1.9 doesn't work exactly like it did in 1.8.
您需要将项目的根目录添加到加载路径.
You need to add the project's root directory to your load path.
您可以通过运行rspec来做到这一点:
You can do this by either running rspec like this:
rspec -I . ./spec/models/tipp_remember_spec.rb
...或通过手动将内容添加到spec_helper.rb
的加载路径中(将其放置在spec_helper.rb
...or by manually adding stuff to the load path in your spec_helper.rb
(put this at the top of your spec_helper.rb
$:<< File.join(File.dirname(__FILE__), '..')
我认为rspec默认情况下也会将您的本地lib
目录也添加到加载路径中,因此您可以改写如下的require行:
I think rspec by default adds your local lib
directory to the load path as well, so you might be able to rewrite the require line as follows instead:
require 'services/my_lib'
这篇关于使用Ruby 1.9.2的RSpec中的require lib会带来“没有此类文件可加载".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!