0

I am learning custom matchers for rspec from this old, 2008 tutorial - http://www.reactive.io/tips/2008/12/10/up-and-running-with-custom-rspec-matchers/

Project Structure

.
├── one_plus.rb
└── one_plus_spec.rb

I followed all the instructions, but I am not able to understand why I am getting the following error:

rspec one_plus_spec.rb
/home/john/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- one_plus (LoadError)
    from /home/john/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /home/john/Code/Rspec/Misc/CustomRspecMatchers/one_plus_spec.rb:3:in `<top (required)>'
    from /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load'
    from /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
    from /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `each'
    from /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load_spec_files'
    from /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:22:in `run'
    from /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:80:in `run'
    from /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:17:in `block in autorun'
john@ubuntu:~/Code/Rspec/Misc/CustomRspecMatchers$

There is no clear answer in other stack overflow questions for this. I don't just want a solution, but I also want to know why this error happens and the concepts that I need to learn to prevent it from happening again.

stack1
  • 1,004
  • 2
  • 13
  • 28

2 Answers2

2

Try using require_relative instead:

require_relative 'one_plus'

./ (current directory) was removed from the load path in Ruby 1.9.

August
  • 12,410
  • 3
  • 35
  • 51
  • 1
    @stack1 It has to do with the load path. You could also modify the load path using the `$LOAD_PATH` variable. – August Jan 03 '15 at 01:04
  • 1
    require will look in the default paths of ruby. You can see which are they executing: `ruby -e 'puts $:'`. So the `one_plus.rb` is not in on of those default paths. Ruby needs to know where is it in order to load it. require_relative says to ruby to use the relative path of the current file as a path to look into. – lcguida Jan 03 '15 at 01:06
  • @rockskull - yes, when I execute the command, I only see paths under `/home/john/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/`. My project path is not in the list. – stack1 Jan 03 '15 at 01:11
  • @August - Yes, `$LOAD_PATH.unshift(File.dirname(__FILE__))` instead of require_relative works too. But, I don't know what it means or what it does. It just works. – stack1 Jan 03 '15 at 01:12
0

your load path doesn't include ./ by default, that's why.

Understanding Ruby's load paths includes details as well as solutions.

I personally use require_relative pretty often in specs myself, but you can also modify the load path.

Community
  • 1
  • 1
erik258
  • 14,701
  • 2
  • 25
  • 31