Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include lib directory in rspec tests

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)


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.


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

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


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