require lib in RSpec with Ruby 1.9.2 brings “no such file to load”

谁说我不能喝 提交于 2019-12-03 02:18:53

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.

You can do this by either running rspec like this:

rspec -I . ./spec/models/tipp_remember_spec.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__), '..')

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'

In RSpec 2.x, the lib directory is automatically added to the load path (see RSpec-Core#get_started).

So you can just use require 'services/my_lib' in your spec file.

If your spec is located spec/models/my_lib_spec.rb and you want to test lib/services/my_lib.rb, then just tell the spec how to get to the lib file

require 'spec_helper'
require_relative '../../lib/services/my_lib'

describe "MyLib" do

  it "should do something" do

Final note: since you're including spec_helper, you typically don't have to give all paths to dependencies, since Rails should load all of these for you.

Try defining the path manually

$LOAD_PATH << './lib/services/'

and then add the library like this

require 'my_lib.rb'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!