supritshah1289
supritshah1289

Reputation: 839

Running a single RSpec spec fails with "`require': cannot load such file"

I am trying to get test-first-ruby-master as instructed but I am getting multiple errors. This is the structure of the files/directory in test-first-ruby-master:

Gemfile     Gemfile.lock    README.md   lib     spec

lib contains ruby files and spec contains spec files. I am trying to execute 00_hello_spec.rb file on my terminal but it doesn't work.

What I have tried

  1. require "lib/00_hello.rb"
  2. require_relative "lib/00_hello.rb"

Errors I get

supritkumars-MacBook-Pro:test-first-ruby-master supritkumarshah$ rspec spec/00_hello_spec.rb 
/Users/supritkumarshah/Desktop/test-first-ruby-master/spec/00_hello_spec.rb:103:in `require': cannot load such file -- lib/00_hello.rb (LoadError)
    from /Users/supritkumarshah/Desktop/test-first-ruby-master/spec/00_hello_spec.rb:103:in `<top (required)>'
    from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1226:in `load'
    from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1226:in `block in load_spec_files'
    from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1224:in `each'
    from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1224:in `load_spec_files'
    from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:97:in `setup'
    from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:85:in `run'
    from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:70:in `run'
    from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:38:in `invoke'
    from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/exe/rspec:4:in `<top (required)>'
    from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/bin/rspec:23:in `load'
    from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/bin/rspec:23:in `<main>'
    from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
    from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'

Thank you

Upvotes: 4

Views: 1967

Answers (3)

fabro
fabro

Reputation: 771

Did you tried adding require 'bundler/setup' at the top of your test helper file?

Upvotes: 0

Dave Schweisguth
Dave Schweisguth

Reputation: 37607

The require is failing because you're requiring a relative path, 'lib/00_hello.rb', but the directory it's relative to (the root of your project) is not in the list of directories that Ruby is looking in for files to require (the load path, $LOAD_PATH).

There are lots of ways to fix this. Here are some:

  • Tell rspec to add the root of your project to the load path. Since that's your current directory, you can use .:

    rspec -I. spec/00_hello_spec.rb
    
  • Change your spec to require_relative '../lib/00_hello'. This works regardless of the current directory or load path — a good idea in command-line programs, although not so important for a learning project.

  • As B Seven answered, change your spec to require './lib/00_hello'. This requires you to run rspec in the root of your project.

  • You can even remove the require altogether! When you run rspec, if the current directory has subdirectories named lib and/or spec, rspec adds them to the load path. This also requires you to run rspec in the root of your project. This solution won't work if you need the require when running your program for real (not under rspec).

Note that including the .rb in the filename you required did not cause the problem, but it's not necessary.

Upvotes: 3

B Seven
B Seven

Reputation: 45943

Although Dave's answer is correct, I prefer to use:

require './lib/00_hello'

Upvotes: 1

Related Questions