32

I am having a problem in testing my gem which includes a lib directory, on JRuby 1.7.4.

I want to test a file located at lib/vger/resources/account_manager.rb

My spec file is in spec/vger/resources/account_manager_spec.rb

require 'spec_helper'

describe Vger::Resources::AccountManager do     
    .
    .
    end 
end

I am trying to include the file which I want to test in spec_helper.rb

require 'rubygems'
require 'bundler/setup'
require 'vger/resources/account_manager'
require 'vger'

RSpec.configure do |config|
  # some (optional) config here
end

While running the test by the command rspec spec/vger/resources/account_manager_spec.rb I am getting this error:

NameError: uninitialized constant Vger::Resources
    const_missing at org/jruby/RubyModule.java:2631

I seems that the file which I want to test is not getting loaded. Please tell me where I am going wrong and where should I make corrections.

Michael Mior
  • 28,107
  • 9
  • 89
  • 113
Akash Khandelwal
  • 346
  • 1
  • 4
  • 10
  • Try to use `require_relative` and path to resources in lib directory from directory where `spec_helper.rb` is. Something like `require_relative "../lib/vger"` instead of `require "vger"` ant etc. – Yevgeniy Anfilofyev Jun 06 '13 at 06:36
  • @YevgeniyAnfilofyev It doesn't seem to work. I tried require_relative '../lib/vger/' also require_relative '../lib/vger/resources/' and remove requre 'Vger' and require "vger/resources/account_manager" – Akash Khandelwal Jun 06 '13 at 06:49
  • 1
    See this question http://stackoverflow.com/questions/11376718/require-lib-in-rspec-with-ruby-1-9-2-brings-no-such-file-to-load – toro2k Jun 06 '13 at 08:01

4 Answers4

21

Manually update your LOAD PATH in spec_helper.rb before calling require should do the trick. Try making this the first line of your spec_helper.rb:

$: << '../lib'

or

$LOAD_PATH << '../lib'

($: is an alias for $LOAD_PATH)

  • I tried it. /home/...../work/vger/lib seem to be getting loaded but still getting the same error – Akash Khandelwal Jun 07 '13 at 04:48
  • 6
    Consider not using `$:` It is very cryptic and since `$LOAD_PATH` is oh so unoffensive to type, so you might as well just use it instead. But +1 for the inclusion. – Csteele5 Jun 20 '16 at 02:10
12

You can add the following line to your .rspec file in app’s root: -I lib

It’s also possible to include files: -r lib/api.rb

These options are described as follows:

-I PATH

Specify PATH to add to $LOAD_PATH (may be used more than once).

-r, --require PATH

Require a file.

Mikhail Vasin
  • 2,421
  • 1
  • 24
  • 31
Slavik Shynkarenko
  • 376
  • 1
  • 5
  • 8
7

I use the following for my specs...depending on which level your lib is....

require_relative '../../lib/module'

Stephane Paul
  • 340
  • 5
  • 4
  • You may wish to improve your answer with a few more details. One of the comments has a bit more info that you could include. – CaptainBli Oct 03 '14 at 13:58
1

RSpec loads rails environment, as I remember, so you just need to add to autoload directories in your application.rb file

Find this line

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

uncomment it fix it to be like this:

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

it should work

Ivan Shamatov
  • 1,406
  • 1
  • 10
  • 17
  • 8
    This is not a rails project. This is a gem which I am using in a rails application. SO, I guess it won't work – Akash Khandelwal Jun 06 '13 at 09:45
  • 1
    I assume this will recursively autoload the path? So for example, any rake tasks under `/lib/tasks` will also be loaded? – Dennis Oct 08 '14 at 13:54