Unable to include file in the spec file - ruby

I am trying to write an RSpec test for a ruby project(Sketchup plugin) but I am facing an issue with the require.
Below is how my folder is structured
In my smoke_tests_spec.rb
require "main_folder/subfolder1/file_to_test.rb"
When I run the rspec using rspec-core/rspec from the root directory I get the following error
Failure/Error: require "main_folder/subfolder1/file_to_test.rb"
LoadError:
cannot load such file -- main_folder/subfolder1/file_to_test.rb

You need to change the LOAD_PATH or use require_relative
Here you have more info.

Related

How to fix "`require_relative': cannot load such file" cannot load - Ruby

Tried to solve this on my own and so far no luck...
Error below:
1: from bin/run:3:in `<main>'
bin/run:3:in `require_relative': cannot load such file -- /Users/jason/Development/code/First Project/bin/bin/environment.rb (LoadError)
What's in my bin folder:
#!/usr/bin/env ruby
require_relative './bin/environment.rb'
CLI.new.call
In my config folder:
require 'pry'
require 'httparty'
require_relative "./lib/api.rb"
require_relative "./lib/cli.rb"
require_relative "./lib/pokemon.rb"
Project Directory:
-->Firstproject
->bin
-run
->config
-environment.rb
->lib
-api.rb
-cli.rb
-pokemon.rb
-gitignore
-Gemfile
-README.md
I think you just need to adjust the path to correctly reference your environment.rb source file:
#!/usr/bin/env ruby
require_relative '../config/environment.rb'
CLI.new.call
If you look closely where your environment.rb file lives, it is in the project_dir/config/environment.rb but your bin/run you are attempting to locate the file at ./bin/environment.rb which doesn't exist according to your Project Directory layout.
Check that your environment.rb file lives in config folder, not in lib. So change it to
require_relative './config/environment.rb'

Why I am getting LoadError for require 'support/number_helper' in Ruby

When I am using the require_relative 'support/number_helper' it's working fine, But when I am using require 'support/number_helper' then I am getting this error.
rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- ./support/number_helper (LoadError)
I also tried this but getting the same error.
require './support/number_helper
I am using the ubuntu.
require_relative 'support/number_helper' searches for a file to load by adding given string to the directory of current_file (__FILE__). For example, in your project folder you have 2 files:
lib/special_gem/fetcher.rb
lib/special_gem/support/number_helper.rb
You can use require_relative 'support/number_helper' in your 1st file to load the 2nd. The command takes the path to the directory of the current file (lib/special_gem/), appends given string (support/number_helper) and successfully finds file to load.
What about require command, if given path is not absolute, it will search for the file in the directories listed in $LOAD_PATH. Very likely your lib folder is in this list, so to load 2nd file you could use the command
require 'special_gem/support/number_helper'
Since it's not relative, you can use it from your 1st file or any other file of your project.
When using require 'support/number_helper' it will search for the file at lib/support/number_helper. If that file is missing, LoadError exception is raised.
See the documentation for details.

Use Ruby Class In A Different Directory

I have written a few ruby classes. However, when trying to access one from another directory I am getting the following error:
uninitialized constant Main::AppVersion
This is what the directory structure looks like:
home --> a --> app_version.rb
home --> b --> c --> lib --> main.rb (and other classes)
Everything within "lib" can see each other. However, when trying to access app_version, it fails. I added the path to app version (home/a) to the $LOAD_PATH. So it should be available from there. I have also tried "requiring" my other class, but when I do that I get the following error:
LoadError: no such file to load -- AppVersion
Any idea on what I could be doing wrong here would be highly appreciated. Thanks!
Can try using require_relative:
require_relative '../../../a/app_version'
You error is:
LoadError: no such file to load -- AppVersion
So require is looking for a file AppVersion.rb. And you said the file name is app_version.rb. Try to load it with:
require 'app_version'
after setting up the $LOAD_PATH.
When you define Main::AppVersion class it's supposed to reside in main folder (i.e. main/app_version.rb)
So it doesn't depend on $LOAD_PATH.
You will have to require the file.
LoadError: no such file to load -- AppVersion
Do you require "app_version" ? Or require "AppVersion"? (first one is correct, and consider to require_relative)
Another option is to run ruby and give the include path like
ruby -Ia -Ib/c/lib b/c/lib/main.rb
.

Ruby including files

I have a Ruby app that runs on a server with no web interface. It is run using the command line(ruby path/to/file.rb).
I have classes in different files that I want to be accessible. The files are located in the "app/classes" directory.
I put this in the application.rb file:
config.autoload_paths += Dir["#{config.root}/classes"]
and I get an uninitialized constant error.
I can put in "require_relitive 'somefile'" but I would rather not have to do this for every class that is used. How do I create an autoload path and where should it be located at?
Use require_all
See https://github.com/jarmo/require_all
It basically allows you to write this:
require 'require_all'
require_all 'app/classes'
And all ruby files in app/classes will be loaded.

How do I require a ruby file from child directory

I have the following structure
tests
specific tests
first_test.rb
more tests
second_test.rb
test_helper.rb
Now I am trying to require test_helper in both first_test and second_test. The following works
require '../test_helper.rb'
and from command line I should be under specific tests and execute
ruby first_test.rb
How can I get more flexibility ie. I want to be in any directory and execute these tests. Currently I am getting 'no such file'. This is not a rails app but in rails I could simply do require 'test_helper.rb'
Bonus points for making this work with rake.
Put your test_helper and other helpers in a separate directory like
/home/XXX/projects/test_project/lib/
or wherever you like and use base directory like
Dir["base/path/to/your/helper/*.rb"].each {|file| require file }
or from my above example
Dir["/home/XXX/projects/test_project/lib/*.rb"].each {|file| require file }
Thats it.
Now add this line to the beginning of every test you want to run separately from any directory.And forget about the directory issue.

Resources