`require': cannot load such file -- spec_helper (LoadError) - ruby

I am creating
bundler gem --test=rspec MyGem.
in which I'm getting the repository structure.
When I try to run the rspec code I get the following error:
`require': cannot load such file -- spec_helper (LoadError)
I then try to apply require relative but I still get an error:
sheetal#ubuntu:~/sheetal/spec$ rspec sheetal_spec.rb
\/home/sheetal/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- spec_helper (LoadError)
from /home/sheetal/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /home/sheetal/sheetal/spec/sheetal_spec.rb:1:in `<top (required)>'
from /home/sheetal/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `load'
from /home/sheetal/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `block in load_spec_files'
from /home/sheetal/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `each'
from /home/sheetal/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `load_spec_files'
from /home/sheetal/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:96:in `setup'
from /home/sheetal/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:84:in `run'
from /home/sheetal/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:69:in `run'
from /home/sheetal/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:37:in `invoke'
from /home/sheetal/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.1.7/exe/rspec:4:in `<top (required)>'
from /home/sheetal/.rvm/gems/ruby-2.2.0/bin/rspec:23:in `load'
from /home/sheetal/.rvm/gems/ruby-2.2.0/bin/rspec:23:in `<main>'
from /home/sheetal/.rvm/gems/ruby-2.2.0/bin/ruby_executable_hooks:15:in `eval'
from /home/sheetal/.rvm/gems/ruby-2.2.0/bin/ruby_executable_hooks:15:in `<main>'

You're running the specs from the spec folder. This messes up the load path. Run specs from the root of your project: ~/sheetal.
rspec spec/sheetal_spec.rb
Rspec adds the spec and lib folders to the load path automatically. If you're already in the spec folder, rspec is going to add spec/spec to the load path instead.
Assuming that you have your code in the lib folder, you would have to add both . and ../lib to the load path if you want to run your tests in the spec folder.

Related

Ruby spreadsheet load error. Why I am getting this error?

I have spreadsheet gem installed. Also configured in the gem file.
When I am executing the test i am getting the following error:
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/spreadsheet-0.9.3/lib/spreadsheet/excel/reader.rb:6:in require': cannot load such file -- spreadsheet/noteObject (LoadError) from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/spreadsheet-0.9.3/lib/spreadsheet/excel/reader.rb:6:in <top (required)>'
from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/spreadsheet-0.9.3/lib/spreadsheet.rb:28:in require' from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/spreadsheet-0.9.3/lib/spreadsheet.rb:28:in <top (required)>'

Terminal error using Test First Ruby

I'm just starting Test First Ruby & I'm having trouble running rakes to start solving problems.
I think it's either my version of RSpec or my version of Ruby that's causing the error.
Here's the error,
AT MacBook-Pro:01_temperature AT$ rake
(in /Users/AT/Desktop/learn_ruby)
/Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- temperature (LoadError)
from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/AT/Desktop/learn_ruby/01_temperature/temperature_spec.rb:18:in `<top (required)>'
from /Library/Ruby/Gems/2.0.0/gems/rspec-core-3.1.2/lib/rspec/core/configuration.rb:1105:in `load'
from /Library/Ruby/Gems/2.0.0/gems/rspec-core-3.1.2/lib/rspec/core/configuration.rb:1105:in `block in load_spec_files'
from /Library/Ruby/Gems/2.0.0/gems/rspec-core-3.1.2/lib/rspec/core/configuration.rb:1105:in `each'
from /Library/Ruby/Gems/2.0.0/gems/rspec-core-3.1.2/lib/rspec/core/configuration.rb:1105:in `load_spec_files'
from /Library/Ruby/Gems/2.0.0/gems/rspec-core-3.1.2/lib/rspec/core/runner.rb:96:in `setup'
from /Library/Ruby/Gems/2.0.0/gems/rspec-core-3.1.2/lib/rspec/core/runner.rb:84:in `run'
from /Library/Ruby/Gems/2.0.0/gems/rspec-core-3.1.2/lib/rspec/core/runner.rb:69:in `run'
from /Library/Ruby/Gems/2.0.0/gems/rspec-core-3.1.2/lib/rspec/core/runner.rb:37:in `invoke'
from /Library/Ruby/Gems/2.0.0/gems/rspec-core-3.1.2/exe/rspec:4:in `<top (required)>'
from /usr/bin/rspec:23:in `load'
from /usr/bin/rspec:23:in `<main>'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby -S rspec /Users/AT/Desktop/learn_ruby/01_temperature/temperature_spec.rb -I/Users/AT/Desktop/learn_ruby/01_temperature -I/Users/AT/Desktop/learn_ruby/01_temperature/solution -f documentation -r ./rspec_config failed
I had a similar problem a few minutes ago but fixed by uninstalling RSpec v-3, and installing v-2.14.
Any help would be appreciated!
Your file temperature_spec is requiring what I presume is the source for the class under test, temperature - but the RSpec loader is not able to resolve the path for this file.
It would be helpful for you to post both the source of your rspec file and your class under test.

How to rackup a config file from outside the root directory?

When I run rackup from within my app directory, it works fine:
walkraft#li234-166:~/discourse$ rackup config.ru
Flushing redis (development mode)
/home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `block in require': iconv will be deprecated in the future, use String#encode instead.
/home/walkraft/discourse/vendor/gems/message_bus/lib/message_bus.rb:130: warning: already initialized constant ENCODE_SITE_TOKEN
>> Thin web server (v1.5.0 codename Knife)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:9292, CTRL+C to stop
However, if I try running rackup from outside this directory:
walkraft#li234-166:~$ rackup discourse/config.ru
/home/walkraft/discourse/config/application.rb:7:in `require': cannot load such file -- ./lib/discourse_plugin_registry (LoadError)
from /home/walkraft/discourse/config/application.rb:7:in `<top (required)>'
from /home/walkraft/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/walkraft/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/walkraft/discourse/config/environment.rb:2:in `<top (required)>'
from /home/walkraft/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/walkraft/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/walkraft/discourse/config.ru:2:in `block in <main>'
from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/lib/rack/builder.rb:51:in `instance_eval'
from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/lib/rack/builder.rb:51:in `initialize'
from /home/walkraft/discourse/config.ru:in `new'
from /home/walkraft/discourse/config.ru:in `<main>'
from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/lib/rack/builder.rb:40:in `eval'
from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/lib/rack/builder.rb:40:in `parse_file'
from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/lib/rack/server.rb:200:in `app'
from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/lib/rack/server.rb:304:in `wrapped_app'
from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/lib/rack/server.rb:254:in `start'
from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/lib/rack/server.rb:137:in `start'
from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/bin/rackup:4:in `<top (required)>'
from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/bin/rackup:19:in `load'
from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/bin/rackup:19:in `<main>'
from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/bin/ruby_noexec_wrapper:14:in `eval'
from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/bin/ruby_noexec_wrapper:14:in `<main>'
How can I run rackup when I'm not inside of the root directory?
If you don't want to manually cd to the Discourse root folder, then why not just add a fix to the rackup config.ru file:
# Insert as first line in config.ru
Dir.chdir(File.dirname(File.expand_path(__FILE__)))
Actually, it's not a problem with rackup; it's a problem with your code.
You have
require './lib/discourse_plugin_registry'
somewhere. This is not ideal. It should rather be something like:
require File.expand_path('../../lib/discourse_plugin_registry', __FILE__)
The way you have it, it uses the current directory explicitly, and no matter what you do to rackup, until you change the current directory - it won't work.

Ruby `require` call fails on custom code

I have found that I have no problem using require to load something like the Sinatra web framework, but I can't seem to use require to load my own custom code?
For example I have two files:
test1.rb
test2.rb
The content of 'test1.rb' is:
#!/usr/bin/env ruby
require 'test2'
The content of 'test2.rb' is:
class String
def vowels
self.scan(/[aeiou]/i)
end
end
And if I try and run ruby test1.rb then I get the following error...
/Users/<username>/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- test2 (LoadError)
from /Users/<username>/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from test1.rb:3:in `<main>'
I then noticed if I tried to use the shotgun gem to load the web server then I get a different but more detailed stack trace of the error...
Boot Error
Something went wrong while loading test1.rb
LoadError: cannot load such file -- test2
/Users/<username>/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
/Users/<username>/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
/Users/<username>/Dropbox/Library/Ruby/Passage/test1.rb:3:in `<top (required)>'
/Users/<username>/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
/Users/<username>/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
/Users/<username>/.rvm/gems/ruby-1.9.3-p125/gems/shotgun-0.9/lib/shotgun/loader.rb:114:in `inner_app'
/Users/<username>/.rvm/gems/ruby-1.9.3-p125/gems/shotgun-0.9/lib/shotgun/loader.rb:102:in `assemble_app'
/Users/<username>/.rvm/gems/ruby-1.9.3-p125/gems/shotgun-0.9/lib/shotgun/loader.rb:86:in `proceed_as_child'
/Users/<username>/.rvm/gems/ruby-1.9.3-p125/gems/shotgun-0.9/lib/shotgun/loader.rb:31:in `call!'
/Users/<username>/.rvm/gems/ruby-1.9.3-p125/gems/shotgun-0.9/lib/shotgun/loader.rb:18:in `call'
/Users/<username>/.rvm/gems/ruby-1.9.3-p125/gems/shotgun-0.9/lib/shotgun/favicon.rb:12:in `call'
/Users/<username>/.rvm/gems/ruby-1.9.3-p125/gems/shotgun-0.9/lib/shotgun/static.rb:14:in `call'
/Users/<username>/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/builder.rb:134:in `call'
/Users/<username>/.rvm/gems/ruby-1.9.3-p125/gems/thin-1.3.1/lib/thin/connection.rb:80:in `block in pre_process'
/Users/<username>/.rvm/gems/ruby-1.9.3-p125/gems/thin-1.3.1/lib/thin/connection.rb:78:in `catch'
/Users/<username>/.rvm/gems/ruby-1.9.3-p125/gems/thin-1.3.1/lib/thin/connection.rb:78:in `pre_process'
/Users/<username>/.rvm/gems/ruby-1.9.3-p125/gems/thin-1.3.1/lib/thin/connection.rb:53:in `process'
/Users/<username>/.rvm/gems/ruby-1.9.3-p125/gems/thin-1.3.1/lib/thin/connection.rb:38:in `receive_data'
/Users/<username>/.rvm/gems/ruby-1.9.3-p125/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run_machine'
/Users/<username>/.rvm/gems/ruby-1.9.3-p125/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run'
/Users/<username>/.rvm/gems/ruby-1.9.3-p125/gems/thin-1.3.1/lib/thin/backends/base.rb:61:in `start'
/Users/<username>/.rvm/gems/ruby-1.9.3-p125/gems/thin-1.3.1/lib/thin/server.rb:159:in `start'
/Users/<username>/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/handler/thin.rb:13:in `run'
/Users/<username>/.rvm/gems/ruby-1.9.3-p125/gems/shotgun-0.9/bin/shotgun:156:in `<top (required)>'
/Users/<username>/.rvm/gems/ruby-1.9.3-p125/bin/shotgun:19:in `load'
/Users/<username>/.rvm/gems/ruby-1.9.3-p125/bin/shotgun:19:in `<main>'
/Users/<username>/.rvm/gems/ruby-1.9.3-p125/bin/ruby_noexec_wrapper:14:in `eval'
/Users/<username>/.rvm/gems/ruby-1.9.3-p125/bin/ruby_noexec_wrapper:14:in `<main>'
I'm not sure what the problem is?
Any help appreciated please.
Thanks.
The current directory (relative to the main ruby file) is not part of the load paths (the set of paths that require looks in). So you either need to add it to the load path (best done by invoking ruby as ruby -I. test1.rb), by using require "./test2.rb" or by using require_relative "test2.rb", which requires files relative to the directory of the file.

Problem using require command

I am simply requiring a file present in the same folder as the testfile however I am getting this wierd message again and again...
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- a.rb (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from C:/dummyFirefox/test_a.rb:1:in `<top (required)>'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `load'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `block in load_spec_files'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `map'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `load_spec_files'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:18:in `run'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:55:in `run_in_process'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:46:in `run'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:10:in `block in autorun'
the code that I write in my testfile test_a.rb is simply:
require 'a.rb'
and when I issue the command : rspec test_a.rb the message is the above mentioned error.
I am using ruby-1.9.2 for development.
Hope I don't miss any details.
Thanks in advance.
In Ruby 1.9.2 the current working directory is no longer part of the load path ($LOAD_PATH or $:). You have to add it manually using:
$LOAD_PATH.unshift '.'
or
$:.unshift '.'
Or you can require the file explicitly:
require './test_a.rb'

Resources