Akash Khandelwal
Akash Khandelwal

Reputation: 346

How to include lib directory in rspec tests

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.

Upvotes: 32

Views: 34074

Answers (4)

Slavik Shynkarenko
Slavik Shynkarenko

Reputation: 386

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.

Upvotes: 13

Stephane Paul
Stephane Paul

Reputation: 350

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

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

Upvotes: 7

user117516
user117516

Reputation:

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)

Upvotes: 21

Ivan Shamatov
Ivan Shamatov

Reputation: 1416

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

Upvotes: 1

Related Questions